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

feat: Allow for dynamic asynchronously rerouting #13186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ export type ClientInit = () => MaybePromise<void>;
* The [`reroute`](https://svelte.dev/docs/kit/hooks#Universal-hooks-reroute) hook allows you to modify the URL before it is used to determine which route to render.
* @since 2.3.0
*/
export type Reroute = (event: { url: URL }) => void | string;
export type Reroute = (event: { url: URL }) => MaybePromise<void | string>;

/**
* The [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook allows you to transport custom types across the server/client boundary.
Expand Down
44 changes: 22 additions & 22 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async function _invalidate() {
if (!pending_invalidate) return;
pending_invalidate = null;

const intent = get_navigation_intent(current.url, true);
const intent = await get_navigation_intent(current.url, true);

// Clear preload, it might be affected by the invalidation.
// Also solves an edge case where a preload is triggered, the navigation for it
Expand Down Expand Up @@ -1168,14 +1168,14 @@ async function load_root_error_page({ status, error, url, route }) {
* @param {URL | undefined} url
* @param {boolean} invalidating
*/
function get_navigation_intent(url, invalidating) {
async function get_navigation_intent(url, invalidating) {
if (!url) return undefined;
if (is_external_url(url, base)) return;

// reroute could alter the given URL, so we pass a copy
let rerouted;
try {
rerouted = app.hooks.reroute({ url: new URL(url) }) ?? url.pathname;
rerouted = (await app.hooks.reroute({ url: new URL(url) })) ?? url.pathname;
} catch (e) {
if (DEV) {
// in development, print the error...
Expand Down Expand Up @@ -1279,7 +1279,7 @@ async function navigate({
accept = noop,
block = noop
}) {
const intent = get_navigation_intent(url, false);
const intent = await get_navigation_intent(url, false);
const nav = _before_navigate({ url, type, delta: popped?.delta, intent });

if (!nav) {
Expand Down Expand Up @@ -1529,15 +1529,15 @@ function setup_preload() {
const target = /** @type {Element} */ (event.target);

clearTimeout(mousemove_timeout);
mousemove_timeout = setTimeout(() => {
preload(target, 2);
mousemove_timeout = setTimeout(async () => {
await preload(target, 2);
}, 20);
});

/** @param {Event} event */
function tap(event) {
async function tap(event) {
if (event.defaultPrevented) return;
preload(/** @type {Element} */ (event.composedPath()[0]), 1);
await preload(/** @type {Element} */ (event.composedPath()[0]), 1);
}

container.addEventListener('mousedown', tap);
Expand All @@ -1559,7 +1559,7 @@ function setup_preload() {
* @param {Element} element
* @param {number} priority
*/
function preload(element, priority) {
async function preload(element, priority) {
const a = find_anchor(element, container);
if (!a) return;

Expand All @@ -1573,19 +1573,19 @@ function setup_preload() {

if (!options.reload && !same_url) {
if (priority <= options.preload_data) {
const intent = get_navigation_intent(url, false);
const intent = await get_navigation_intent(url, false);
if (intent) {
if (DEV) {
_preload_data(intent).then((result) => {
if (result.type === 'loaded' && result.state.error) {
console.warn(
`Preloading data for ${intent.url.pathname} failed with the following error: ${result.state.error.message}\n` +
'If this error is transient, you can ignore it. Otherwise, consider disabling preloading for this route. ' +
'This route was preloaded due to a data-sveltekit-preload-data attribute. ' +
'See https://svelte.dev/docs/kit/link-options for more info'
);
}
});
const results = await _preload_data(intent);

if (results.type === 'loaded' && results.state.error) {
console.warn(
`Preloading data for ${intent.url.pathname} failed with the following error: ${results.state.error.message}\n` +
'If this error is transient, you can ignore it. Otherwise, consider disabling preloading for this route. ' +
'This route was preloaded due to a data-sveltekit-preload-data attribute. ' +
'See https://svelte.dev/docs#preload for more info'
);
}
} else {
_preload_data(intent);
}
Expand Down Expand Up @@ -1820,7 +1820,7 @@ export async function preloadData(href) {
}

const url = resolve_url(href);
const intent = get_navigation_intent(url, false);
const intent = await get_navigation_intent(url, false);

if (!intent) {
throw new Error(`Attempted to preload a URL that does not belong to this app: ${url}`);
Expand Down Expand Up @@ -2367,7 +2367,7 @@ async function _hydrate(
if (!__SVELTEKIT_EMBEDDED__) {
// See https://github.com/sveltejs/kit/pull/4935#issuecomment-1328093358 for one motivation
// of determining the params on the client side.
({ params = {}, route = { id: null } } = get_navigation_intent(url, false) || {});
({ params = {}, route = { id: null } } = (await get_navigation_intent(url, false)) || {});
}

/** @type {import('./types.js').NavigationFinished | undefined} */
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function respond(request, options, manifest, state) {
// reroute could alter the given URL, so we pass a copy
let rerouted_path;
try {
rerouted_path = options.hooks.reroute({ url: new URL(url) }) ?? url.pathname;
rerouted_path = (await options.hooks.reroute({ url: new URL(url) })) ?? url.pathname;
} catch {
return text('Internal Server Error', {
status: 500
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ declare module '@sveltejs/kit' {
* The [`reroute`](https://svelte.dev/docs/kit/hooks#Universal-hooks-reroute) hook allows you to modify the URL before it is used to determine which route to render.
* @since 2.3.0
*/
export type Reroute = (event: { url: URL }) => void | string;
export type Reroute = (event: { url: URL }) => MaybePromise<void | string>;

/**
* The [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook allows you to transport custom types across the server/client boundary.
Expand Down
Loading