Skip to content

Commit a2fe7ab

Browse files
authored
generate __data.json files as siblings of .html files (#11269)
* generate __data.json files as siblings of .html files * duplicate cookies in .html case --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 9c42232 commit a2fe7ab

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

.changeset/happy-beds-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': major
3+
---
4+
5+
breaking: generate `__data.json` files as sibling to `.html` files

packages/kit/src/runtime/server/cookie.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, serialize } from 'cookie';
2-
import { normalize_path, resolve } from '../../utils/url.js';
2+
import { add_data_suffix, normalize_path, resolve } from '../../utils/url.js';
33

44
/**
55
* Tracks all cookies set during dev mode so we can emit warnings
@@ -245,6 +245,14 @@ export function add_cookies_to_headers(headers, cookies) {
245245
for (const new_cookie of cookies) {
246246
const { name, value, options } = new_cookie;
247247
headers.append('set-cookie', serialize(name, value, options));
248+
249+
// special case — for routes ending with .html, the route data lives in a sibling
250+
// `.html__data.json` file rather than a child `/__data.json` file, which means
251+
// we need to duplicate the cookie
252+
if (options.path.endsWith('.html')) {
253+
const path = add_data_suffix(options.path);
254+
headers.append('set-cookie', serialize(name, value, { ...options, path }));
255+
}
248256
}
249257
}
250258

packages/kit/src/runtime/server/page/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export async function render_page(event, page, options, manifest, state, resolve
7878

7979
// it's crucial that we do this before returning the non-SSR response, otherwise
8080
// SvelteKit will erroneously believe that the path has been prerendered,
81-
// causing functions to be omitted from the manifesst generated later
81+
// causing functions to be omitted from the manifest generated later
8282
const should_prerender = get_option(nodes, 'prerender') ?? false;
8383
if (should_prerender) {
8484
const mod = leaf_node.server;

packages/kit/src/utils/url.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,24 @@ function allow_nodejs_console_log(url) {
208208
}
209209

210210
const DATA_SUFFIX = '/__data.json';
211+
const HTML_DATA_SUFFIX = '.html__data.json';
211212

212213
/** @param {string} pathname */
213214
export function has_data_suffix(pathname) {
214-
return pathname.endsWith(DATA_SUFFIX);
215+
return pathname.endsWith(DATA_SUFFIX) || pathname.endsWith(HTML_DATA_SUFFIX);
215216
}
216217

217218
/** @param {string} pathname */
218219
export function add_data_suffix(pathname) {
220+
if (pathname.endsWith('.html')) return pathname.replace(/\.html$/, HTML_DATA_SUFFIX);
219221
return pathname.replace(/\/$/, '') + DATA_SUFFIX;
220222
}
221223

222224
/** @param {string} pathname */
223225
export function strip_data_suffix(pathname) {
226+
if (pathname.endsWith(HTML_DATA_SUFFIX)) {
227+
return pathname.slice(0, -HTML_DATA_SUFFIX.length) + '.html';
228+
}
229+
224230
return pathname.slice(0, -DATA_SUFFIX.length);
225231
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load() {
2+
return {
3+
message: 'hello'
4+
};
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>{data.message}</h1>

0 commit comments

Comments
 (0)