Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate __data.json files as siblings of .html files #11269

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-beds-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: generate `__data.json` files as sibling to `.html` files
10 changes: 9 additions & 1 deletion packages/kit/src/runtime/server/cookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, serialize } from 'cookie';
import { normalize_path, resolve } from '../../utils/url.js';
import { add_data_suffix, normalize_path, resolve } from '../../utils/url.js';

/**
* Tracks all cookies set during dev mode so we can emit warnings
Expand Down Expand Up @@ -245,6 +245,14 @@ export function add_cookies_to_headers(headers, cookies) {
for (const new_cookie of cookies) {
const { name, value, options } = new_cookie;
headers.append('set-cookie', serialize(name, value, options));

// special case — for routes ending with .html, the route data lives in a sibling
// `.html__data.json` file rather than a child `/__data.json` file, which means
// we need to duplicate the cookie
if (options.path.endsWith('.html')) {
const path = add_data_suffix(options.path);
headers.append('set-cookie', serialize(name, value, { ...options, path }));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function render_page(event, page, options, manifest, state, resolve

// it's crucial that we do this before returning the non-SSR response, otherwise
// SvelteKit will erroneously believe that the path has been prerendered,
// causing functions to be omitted from the manifesst generated later
// causing functions to be omitted from the manifest generated later
const should_prerender = get_option(nodes, 'prerender') ?? false;
if (should_prerender) {
const mod = leaf_node.server;
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,24 @@ function allow_nodejs_console_log(url) {
}

const DATA_SUFFIX = '/__data.json';
const HTML_DATA_SUFFIX = '.html__data.json';

/** @param {string} pathname */
export function has_data_suffix(pathname) {
return pathname.endsWith(DATA_SUFFIX);
return pathname.endsWith(DATA_SUFFIX) || pathname.endsWith(HTML_DATA_SUFFIX);
}

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

/** @param {string} pathname */
export function strip_data_suffix(pathname) {
if (pathname.endsWith(HTML_DATA_SUFFIX)) {
return pathname.slice(0, -HTML_DATA_SUFFIX.length) + '.html';
}

return pathname.slice(0, -DATA_SUFFIX.length);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function load() {
return {
message: 'hello'
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<h1>{data.message}</h1>
Loading