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

chore: improve error message for prefetching invalid url #7314

Merged
merged 4 commits into from Oct 20, 2022

Conversation

dominikg
Copy link
Member

Happened to come across Uncaught (in promise) Error: Attempted to prefetch a URL that does not belong to this app in an app today and didn't immediately find the cause.

The url in the message can help finding where in the application it happened, also added some guidance and docs link for good measure.

In my case it was a link to a page that hadn't been created yet. If thats the only way to reach this error, maybe the url is enough to not confuse users into disabling prefetching too eagerly?

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. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Oct 19, 2022

🦋 Changeset detected

Latest commit: c3d9c27

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

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

@dominikg dominikg changed the title chore: improve error message for prerendering invalid url chore: improve error message for prefetching invalid url Oct 19, 2022
@@ -199,7 +199,9 @@ export function create_client({ target, base, trailing_slash }) {
const intent = get_navigation_intent(url, false);

if (!intent) {
throw new Error('Attempted to prefetch a URL that does not belong to this app');
throw new Error(
`Attempted to prefetch "${url}", a URL that does not belong to this app. To disable prefetching add data-sveltekit-prefetch="off" to a parent. See https://kit.svelte.dev/docs/link-options#data-sveltekit-prefetch for more information.`
Copy link
Member

Choose a reason for hiding this comment

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

IIUC the error shouldn't happen if prefetching happened as a result of the user stopping over a link, but only if the app calls prefetch(bad_url). If that's not currently the case then I'd argue it's a bug, and we should fix that instead

Copy link
Member Author

Choose a reason for hiding this comment

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

In my case it was a newly added href="/some/new/route" . The error happened during dev after the href was added but before +page.svelte was created. So bad url. Maybe skip and warn instead during dev and error only in build? But adding the url to the message seems helpful.

Copy link
Member

Choose a reason for hiding this comment

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

right, but what i'm saying is that data-sveltekit-prefetch shouldn't cause that error, it should be reserved for when you do prefetch('/some/new/route')

Copy link
Member

Choose a reason for hiding this comment

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

So .. we have a bug hiding somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh. But adding the url value to the error message helps that case as well. The stacktrace isn't helping because it's internal to kit, so you have to start debugging to get a clue.

I have removed the extra information to avoid confusion.

@Rich-Harris Rich-Harris merged commit ffd6fa7 into sveltejs:master Oct 20, 2022
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.

None yet

3 participants