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

throw if url.search or url.searchParams is accessed from page store during prerendering #3314

Merged
merged 4 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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/funny-zebras-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Throw on accessing url.search/searchParams from page store during prerendering
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export async function respond(incoming, options, state = {}) {
url: request.url,
params: request.params,
options,
state,
$session: await options.hooks.getSession(request),
page_config: { router: true, hydrate: true },
stuff: {},
Expand Down
14 changes: 2 additions & 12 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { respond } from '../index.js';
import { s } from '../../../utils/misc.js';
import { escape_json_string_in_html } from '../../../utils/escape.js';
import { is_root_relative, resolve } from '../../../utils/url.js';
import { create_url_proxy } from './utils.js';

/**
* @param {{
Expand All @@ -15,7 +16,6 @@ import { is_root_relative, resolve } from '../../../utils/url.js';
* node: import('types/internal').SSRNode;
* $session: any;
* stuff: Record<string, any>;
* prerender_enabled: boolean;
* is_error: boolean;
* status?: number;
* error?: Error;
Expand All @@ -32,7 +32,6 @@ export async function load_node({
node,
$session,
stuff,
prerender_enabled,
is_error,
status,
error
Expand All @@ -57,19 +56,10 @@ export async function load_node({

let loaded;

const url_proxy = new Proxy(url, {
get: (target, prop, receiver) => {
if (prerender_enabled && (prop === 'search' || prop === 'searchParams')) {
throw new Error('Cannot access query on a page with prerendering enabled');
}
return Reflect.get(target, prop, receiver);
}
});

if (module.load) {
/** @type {import('types/page').LoadInput | import('types/page').ErrorLoadInput} */
const load_input = {
url: url_proxy,
url: state.prerender ? create_url_proxy(url) : url,
params,
get session() {
uses_credentials = true;
Expand Down
11 changes: 10 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { coalesce_to_error } from '../../../utils/error.js';
import { hash } from '../../hash.js';
import { escape_html_attr } from '../../../utils/escape.js';
import { s } from '../../../utils/misc.js';
import { create_url_proxy } from './utils.js';

// TODO rename this function/module

/**
* @param {{
* branch: Array<import('./types').Loaded>;
* options: import('types/internal').SSRRenderOptions;
* state: import('types/internal').SSRRenderState;
* $session: any;
* page_config: { hydrate: boolean, router: boolean };
* status: number;
Expand All @@ -24,6 +26,7 @@ import { s } from '../../../utils/misc.js';
export async function render_response({
branch,
options,
state,
$session,
page_config,
status,
Expand Down Expand Up @@ -73,7 +76,13 @@ export async function render_response({
navigating: writable(null),
session
},
page: { url, params, status, error, stuff },
page: {
url: state.prerender ? create_url_proxy(url) : url,
params,
status,
error,
stuff
},
components: branch.map(({ node }) => node.module.default)
};

Expand Down
4 changes: 1 addition & 3 deletions packages/kit/src/runtime/server/page/respond.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render_response } from './render.js';
import { load_node } from './load_node.js';
import { is_prerender_enabled, respond_with_error } from './respond_with_error.js';
import { respond_with_error } from './respond_with_error.js';
import { coalesce_to_error } from '../../../utils/error.js';

/**
Expand Down Expand Up @@ -105,7 +105,6 @@ export async function respond(opts) {
url: request.url,
node,
stuff,
prerender_enabled: is_prerender_enabled(options, node, state),
is_error: false
});

Expand Down Expand Up @@ -160,7 +159,6 @@ export async function respond(opts) {
url: request.url,
node: error_node,
stuff: node_loaded.stuff,
prerender_enabled: is_prerender_enabled(options, error_node, state),
is_error: true,
status,
error
Expand Down
15 changes: 1 addition & 14 deletions packages/kit/src/runtime/server/page/respond_with_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { coalesce_to_error } from '../../../utils/error.js';

/**
* @typedef {import('./types.js').Loaded} Loaded
* @typedef {import('types/internal').SSRNode} SSRNode
* @typedef {import('types/internal').SSRRenderOptions} SSRRenderOptions
* @typedef {import('types/internal').SSRRenderState} SSRRenderState
*/
Expand Down Expand Up @@ -47,7 +46,6 @@ export async function respond_with_error({
node: default_layout,
$session,
stuff: {},
prerender_enabled: is_prerender_enabled(options, default_error, state),
is_error: false
})
);
Expand All @@ -63,7 +61,6 @@ export async function respond_with_error({
node: default_error,
$session,
stuff: layout_loaded ? layout_loaded.stuff : {},
prerender_enabled: is_prerender_enabled(options, default_error, state),
is_error: true,
status,
error
Expand All @@ -72,6 +69,7 @@ export async function respond_with_error({

return await render_response({
options,
state,
$session,
page_config: {
hydrate: options.hydrate,
Expand All @@ -97,14 +95,3 @@ export async function respond_with_error({
};
}
}

/**
* @param {SSRRenderOptions} options
* @param {SSRNode} node
* @param {SSRRenderState} state
*/
export function is_prerender_enabled(options, node, state) {
return (
options.prerender && (!!node.module.prerender || (!!state.prerender && state.prerender.all))
);
}
11 changes: 11 additions & 0 deletions packages/kit/src/runtime/server/page/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @param {URL} url */
export function create_url_proxy(url) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
return new Proxy(url, {
get: (target, prop, receiver) => {
if (prop === 'search' || prop === 'searchParams') {
throw new Error(`Cannot access url.${prop} on a page with prerendering enabled`);
}
return Reflect.get(target, prop, receiver);
}
});
}