Skip to content

Commit

Permalink
fix: make $app/navigation more resilient to bundler reordering (#9808)
Browse files Browse the repository at this point in the history
* fix: make $app/navigation more resilient to bundler reordering

* rename invalidate_all

* changeset

---------

Co-authored-by: Rich Harris <git@rich-harris.dev>
  • Loading branch information
gtm-nayan and Rich Harris committed May 4, 2023
1 parent d8ec10d commit 285c606
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-tigers-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: make $app/navigation more resilient to bundler reordering
16 changes: 3 additions & 13 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import * as devalue from 'devalue';
import { client } from '../client/singletons.js';
import { DEV } from 'esm-env';
import { client_method } from '../client/singletons.js';
import { invalidateAll } from './navigation.js';
import { BROWSER, DEV } from 'esm-env';

/**
* @param {string} name
*/
function guard(name) {
return () => {
throw new Error(`Cannot call ${name}(...) on the server`);
};
}

/** @type {import('$app/forms').applyAction} */
export const applyAction = BROWSER ? client.apply_action : guard('applyAction');
export const applyAction = client_method('apply_action');

/** @type {import('$app/forms').deserialize} */
export function deserialize(result) {
Expand Down
39 changes: 17 additions & 22 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { BROWSER } from 'esm-env';
import { client } from '../client/singletons.js';

/**
* @param {string} name
*/
function guard(name) {
return () => {
throw new Error(`Cannot call ${name}(...) on the server`);
};
}

export const disableScrollHandling = BROWSER
? client.disable_scroll_handling
: guard('disableScrollHandling');
export const goto = BROWSER ? client.goto : guard('goto');
export const invalidate = BROWSER ? client.invalidate : guard('invalidate');
export const invalidateAll = BROWSER ? client.invalidateAll : guard('invalidateAll');
export const preloadData = BROWSER ? client.preload_data : guard('preloadData');
export const preloadCode = BROWSER ? client.preload_code : guard('preloadCode');
export const beforeNavigate = BROWSER ? client.before_navigate : () => {};
export const afterNavigate = BROWSER ? client.after_navigate : () => {};
import { client_method } from '../client/singletons.js';

export const disableScrollHandling = /* @__PURE__ */ client_method('disable_scroll_handling');

export const goto = /* @__PURE__ */ client_method('goto');

export const invalidate = /* @__PURE__ */ client_method('invalidate');

export const invalidateAll = /* @__PURE__ */ client_method('invalidate_all');

export const preloadData = /* @__PURE__ */ client_method('preload_data');

export const preloadCode = /* @__PURE__ */ client_method('preload_code');

export const beforeNavigate = /* @__PURE__ */ client_method('before_navigate');

export const afterNavigate = /* @__PURE__ */ client_method('after_navigate');
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ export function create_client(app, target) {
return invalidate();
},

invalidateAll: () => {
invalidate_all: () => {
force_invalidation = true;
return invalidate();
},
Expand Down
30 changes: 30 additions & 0 deletions packages/kit/src/runtime/client/singletons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { writable } from 'svelte/store';
import { create_updated_store, notifiable_store } from './utils.js';
import { BROWSER } from 'esm-env';

/** @type {import('./types').Client} */
export let client;
Expand All @@ -13,6 +14,35 @@ export function init(opts) {
client = opts.client;
}

/**
* @template {keyof typeof client} T
* @param {T} key
* @returns {typeof client[T]}
*/
export function client_method(key) {
if (!BROWSER) {
if (key === 'before_navigate' || key === 'after_navigate') {
// @ts-expect-error doesn't recognize that both keys here return void so expects a async function
return () => {};
} else {
/** @type {Record<string, string>} */
const name_lookup = {
disable_scroll_handling: 'disableScrollHandling',
preload_data: 'preloadData',
preload_code: 'preloadCode',
invalidate_all: 'invalidateAll'
};

return () => {
throw new Error(`Cannot call ${name_lookup[key] ?? key}(...) on the server`);
};
}
} else {
// @ts-expect-error
return (...args) => client[key](...args);
}
}

export const stores = {
url: notifiable_store({}),
page: notifiable_store({}),
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface Client {
disable_scroll_handling(): void;
goto: typeof goto;
invalidate: typeof invalidate;
invalidateAll: typeof invalidateAll;
invalidate_all: typeof invalidateAll;
preload_code: typeof preloadCode;
preload_data: typeof preloadData;
apply_action: typeof applyAction;
Expand Down

0 comments on commit 285c606

Please sign in to comment.