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
get it working
  • Loading branch information
Rich-Harris committed Aug 26, 2022
commit e30390024b30e4329bef201762528aaefbd01d4c
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.1",
"cookie": "^0.5.0",
"devalue": "^3.0.1",
"devalue": "^3.1.0",
"kleur": "^4.1.4",
"magic-string": "^0.26.2",
"mime": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function render_data(event, route, options, state) {
);

url.searchParams.delete('__invalid');
url.searchParams.delete('__id');

const new_event = { ...event, url };

Expand Down
23 changes: 23 additions & 0 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ export async function render_response({
/** @param {string} path */
const prefixed = (path) => (path.startsWith('/') ? path : `${assets}/${path}`);

const serialized = { data: '', errors: 'null' };

try {
serialized.data = devalue(branch.map(({ server_data }) => server_data));
} catch (e) {
// TODO if we wanted to get super fancy we could track down the origin of the `load`
// function, but it would mean passing more stuff around than we currently do
const error = /** @type {any} */ (e);
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const match = /\[(\d+)\]\.data\.(.+)/.exec(error.path);
if (match) throw new Error(`${error.message} (data.${match[2]})`);
throw error;
}

if (validation_errors) {
try {
serialized.errors = devalue(validation_errors);
} catch (e) {
const error = /** @type {any} */ (e);
if (error.path) throw new Error(`${error.message} (errors.${error.path})`);
throw error;
}
}

// prettier-ignore
const init_app = `
import { set_public_env, start } from ${s(prefixed(entry.file))};
Expand Down
18 changes: 13 additions & 5 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,17 @@ export function allowed_methods(mod) {

/** @param {any} data */
export function data_response(data) {
return new Response(`window.__sveltekit_data = ${devalue(data)}`, {
headers: {
'content-type': 'application/javascript'
}
});
try {
return new Response(`window.__sveltekit_data = ${devalue(data)}`, {
headers: {
'content-type': 'application/javascript'
}
});
} catch (e) {
return new Response(`throw new Error(${JSON.stringify(e.message)})`, {
headers: {
'content-type': 'application/javascript'
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
class Nope {
toString() {
return 'should not see me'
}
}

export function load() {
return {
regex: /nope/
nope: new Nope()
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
export let data;
</script>

<h1>{data.regex.test('nope')}</h1>
<h1>{data.nope}</h1>
4 changes: 2 additions & 2 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ test.describe('Shadowed pages', () => {

expect(await page.textContent('h1')).toBe('500');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "data.regex returned from \'load\' in src/routes/shadowed/serialization/+page.server.js cannot be serialized as JSON"'
'This is your custom error page saying: "Cannot stringify arbitrary non-POJOs (data.nope)"'
);
});
}
Expand Down Expand Up @@ -579,7 +579,7 @@ test.describe('Errors', () => {
expect(lines[1]).toContain('+page.server.js:4:8');
}

const error = read_errors('/errors/page-endpoint/get-implicit');
const error = read_errors('/errors/page-endpoint/get-implicit/__data.js');
expect(error).toContain('oops');
});

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.