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

fix: make $app/navigation more resilient to bundler reordering #9808

Merged
merged 4 commits into from
May 4, 2023
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
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('invalidateAll');

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');
1 change: 1 addition & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,7 @@ export function create_client(app, target) {
return invalidate();
},

// TODO: Why is only this camel cased?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular good reason, as far as I can tell, since some other things from here that are exposed in packages/kit/src/runtime/app/navigation.js are snake case in this file and then camel case in that file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in #6493. I assume it was just a tiny slip-up. I'd be in favor of switching to invalidate_all.

invalidateAll: () => {
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