Skip to content

fix: handling 404 when redirect from +layout.ts in SPA mode #11771

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

Closed
wants to merge 3 commits into from
Closed
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
45 changes: 28 additions & 17 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,15 +1060,6 @@ async function load_root_error_page({ status, error, url, route }) {
}
}

const root_layout = await load_node({
loader: default_layout_loader,
url,
params,
route,
parent: () => Promise.resolve({}),
server_data_node: create_data_node(server_data_node)
});

/** @type {import('./types.js').BranchNode} */
const root_error = {
node: await default_error_loader(),
Expand All @@ -1078,14 +1069,34 @@ async function load_root_error_page({ status, error, url, route }) {
data: null
};

return await get_navigation_result_from_branch({
url,
params,
branch: [root_layout, root_error],
status,
error,
route: null
});
try {
const root_layout = await load_node({
loader: default_layout_loader,
url,
params,
route,
parent: () => Promise.resolve({}),
server_data_node: create_data_node(server_data_node)
});

return await get_navigation_result_from_branch({
url,
params,
branch: [root_layout, root_error],
status,
error,
route: null
});
} catch (err) {
return await get_navigation_result_from_branch({
url,
params,
branch: [root_error],
status,
error,
route: null
});
}
Comment on lines +1090 to +1099
Copy link
Member

@eltigerchino eltigerchino Mar 19, 2024

Choose a reason for hiding this comment

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

I don't think this is quite right. We need to differentiate between Redirect errors and other errors instead of loading the default error page without the root layout results (which would cause more errors when it loads without the expected layout data). A redirect should navigate to the redirected URL while other errors should render the static fallback error page (but the client doesn't have access to that so I think we just use server_fallback or native_navigation?)

}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/kit/test/apps/no-ssr/src/routes/+layout.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
import { redirect } from '@sveltejs/kit';

export const ssr = false;

export async function load({ route }) {
//@ts-ignore
if (route.id == '/bad-route' || route.id == '/good-route') {
Copy link
Member

Choose a reason for hiding this comment

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

route.id will always return null if the route doesn't exist. We should check for url.pathname instead so that this condition passes when we navigate to /bad-route.

throw redirect(302, '/');
}
}
Empty file.
9 changes: 9 additions & 0 deletions packages/kit/test/apps/no-ssr/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ test('navigating to a non-existent route renders the default error page', async
await page.waitForLoadState('networkidle');
expect(await page.textContent('h1')).toBe('404');
});

test('error occur in load function at root layout renders the default error page', async ({
page
}) => {
test.setTimeout(3000);
await page.goto('/bad-route');
await page.waitForLoadState('networkidle');
expect(await page.textContent('h1')).toBe('404');
});