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

Conversation

manhhungpc
Copy link

@manhhungpc manhhungpc commented Feb 1, 2024

fixes #11099

When redirect to an unknow route in SPA mode, which has a root load function in +layout.ts, it will fallback to the root +error.svelte instead of a blank page.
Issue #11494 seems to be releated


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Feb 1, 2024

⚠️ No Changeset found

Latest commit: cf60aeb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@eltigerchino
Copy link
Member

eltigerchino commented Feb 15, 2024

Thanks for opening this PR, although I’m not sure if this is the right solution. We need to load the root layout when rendering the root error page. I’m guessing the tests are failing because of the root layout node being omitted. I think we should be catching the error instead as I explained here #11099 (comment) adding a test for this scenario should help with testing the solution

@manhhungpc
Copy link
Author

Thanks for pointing that out! And I'm working on your solution

@benmccann benmccann marked this pull request as draft February 18, 2024 16:27
@benmccann
Copy link
Member

I've marked this as a draft for now. Please hit "ready for review" when it's ready for another look

Comment on lines +1090 to +1099
} catch (err) {
return await get_navigation_result_from_branch({
url,
params,
branch: [root_error],
status,
error,
route: null
});
}
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?)


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.

@eltigerchino
Copy link
Member

closed in favour of #12005

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SPA Mode: Redirect from +layout.ts doesn't work on 404
3 participants