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

Allow server-only load functions to return more than JSON #6318

Merged
merged 25 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wire things up
  • Loading branch information
Rich-Harris committed Aug 26, 2022
commit fe30204ffa9d6ce8f8cb61f5e398df36795f67bd
22 changes: 11 additions & 11 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,20 +700,20 @@ export function create_client({ target, base, trailing_slash }) {

if (route.uses_server_data && invalid_server_nodes.some(Boolean)) {
try {
const res = await native_fetch(
`${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json${url.search}`,
{
headers: {
'x-sveltekit-invalidated': invalid_server_nodes.map((x) => (x ? '1' : '')).join(',')
}
}
const data_url = new URL(url);
data_url.pathname += `${url.pathname.endsWith('/') ? '' : '/'}__data.js`;
data_url.searchParams.set(
'__invalid',
invalid_server_nodes.map((x) => (x ? 'y' : 'n')).join('')
);

server_data = /** @type {import('types').ServerData} */ (await res.json());
await import(/* @vite-ignore */ data_url.href);

if (!res.ok) {
throw server_data;
}
// @ts-expect-error
server_data = /** @type {import('types').ServerData} */ (window.__sveltekit_data);

// @ts-expect-error
delete window.__sveltekit_data;
} catch (e) {
// something went catastrophically wrong — bail and defer to the server
native_navigation(url);
Expand Down
16 changes: 9 additions & 7 deletions packages/kit/src/runtime/server/data/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HttpError, Redirect } from '../../../index/private.js';
import { HttpError, Redirect } from '../../control.js';
import { normalize_error } from '../../../utils/error.js';
import { once } from '../../../utils/functions.js';
import { load_server_data } from '../page/load_data.js';
import { error_to_pojo } from '../utils.js';
import { data_response, error_to_pojo } from '../utils.js';
import devalue from 'devalue';

/**
Expand All @@ -24,8 +24,10 @@ export async function render_data(event, route, options, state) {
const node_ids = [...route.page.layouts, route.page.leaf];

const invalidated =
event.request.headers.get('x-sveltekit-invalidated')?.split(',').map(Boolean) ??
node_ids.map(() => true);
event.url.searchParams
.get('__invalid')
?.split('')
.map((x) => x === 'y') ?? node_ids.map(() => true);

let aborted = false;

Expand Down Expand Up @@ -112,7 +114,7 @@ export async function render_data(event, route, options, state) {
nodes: nodes.slice(0, length)
};

return new Response(`window.__data = ${devalue(server_data)}`);
return data_response(server_data);
} catch (e) {
const error = normalize_error(e);

Expand All @@ -123,10 +125,10 @@ export async function render_data(event, route, options, state) {
location: error.location
};

return new Response(`window.__data = ${devalue(server_data)}`);
return data_response(server_data);
} else {
// TODO make it clearer that this was an unexpected error
return new Response(`window.__data = ${devalue(error_to_pojo(error, options.get_stack))}`);
return data_response(error_to_pojo(error, options.get_stack));
}
}
}
6 changes: 5 additions & 1 deletion packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ export function allowed_methods(mod) {

/** @param {any} data */
export function data_response(data) {
return new Response(`window.__data = ${devalue(data)}`);
return new Response(`window.__sveltekit_data = ${devalue(data)}`, {
headers: {
'content-type': 'application/javascript'
}
});
}