Skip to content

Commit

Permalink
[fix] avoid unnecessary $page store updates (#8457)
Browse files Browse the repository at this point in the history
Clicking a page link and staying on the same page with nothing changing still changed the $page.store, because
- form was always !== undefined (it's null)
- $page.error was set to undefined after hydration, but it should be null
  • Loading branch information
dummdidumm committed Jan 11, 2023
1 parent c413753 commit ce02847
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-otters-swim.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] avoid unnecessary $page store updates
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Expand Up @@ -467,7 +467,7 @@ export function create_client({ target, base }) {
!current.url ||
url.href !== current.url.href ||
current.error !== error ||
form !== undefined ||
(form !== undefined && form !== page.form) ||
data_changed;

if (page_changed) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/types.d.ts
Expand Up @@ -26,7 +26,7 @@ export interface Client {
// private API
_hydrate(opts: {
status: number;
error: App.Error;
error: App.Error | null;
node_ids: number[];
params: Record<string, string>;
route: { id: string | null };
Expand Down
13 changes: 7 additions & 6 deletions packages/kit/src/runtime/server/page/render.js
Expand Up @@ -158,7 +158,7 @@ export async function render_response({
/** @param {string} path */
const prefixed = (path) => (path.startsWith('/') ? path : `${assets}/${path}`);

const serialized = { data: '', form: 'null' };
const serialized = { data: '', form: 'null', error: 'null' };

try {
serialized.data = `[${branch
Expand Down Expand Up @@ -196,6 +196,10 @@ export async function render_response({
serialized.form = uneval_action_response(form_value, /** @type {string} */ (event.route.id));
}

if (error) {
serialized.error = devalue.uneval(error);
}

if (inline_styles.size > 0) {
const content = Array.from(inline_styles.values()).join('\n');

Expand Down Expand Up @@ -256,17 +260,14 @@ export async function render_response({
const hydrate = [
`node_ids: [${branch.map(({ node }) => node.index).join(', ')}]`,
`data: ${serialized.data}`,
`form: ${serialized.form}`
`form: ${serialized.form}`,
`error: ${serialized.error}`
];

if (status !== 200) {
hydrate.push(`status: ${status}`);
}

if (error) {
hydrate.push(`error: ${devalue.uneval(error)}`);
}

if (options.embedded) {
hydrate.push(`params: ${devalue.uneval(event.params)}`, `route: ${s(event.route)}`);
}
Expand Down

0 comments on commit ce02847

Please sign in to comment.