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: always default paths.assets to paths.base #8928

Merged
merged 6 commits into from
Feb 8, 2023

Conversation

jdkula
Copy link
Contributor

@jdkula jdkula commented Feb 7, 2023

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
    • tests/prerendering/paths-base was modified to reproduce the behavior in the issue linked below, which fails before the fix and passes after.

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:.

This fixes the issue discussed in #8919 where the build process will fail if trailingSlash = 'never' in the root page and a paths.base is set. Essentially, the build process would eat the last element of the base path and subsequently error out.

When trailingSlash = 'never';, when prerendering /$base/ (our root), the prerenderer gets redirected to '/$base' at prerender.js:305 (which strikes me as invalid, it's kind of "before the root"). Then, that will cause us to visit /$base, which will return the HTML we want rendered at the /$base. This is a problem though, because at this point all relative links we crawl will be resolved relative to /$base, so when we call resolve at prerender.js:250 with, for example, ./favicon.png, we'll end up with a call like resolve('/$base', './favicon.png'), which will ofc resolve to /favicon.png (and not /$base/favicon.ico, which is what we want).

This was resolved by adding a special case in prerender.js's visit function that, if the current URL matches the config.paths.base url exactly, then it will add a trailing slash (causing relative paths to resolve as children to the root/base directory rather than as siblings).

@jdkula jdkula changed the title Fix prerendering error when accessing assets from the root page while trailingSlash is never and a base path is set fix: prerendering error when accessing assets from the root page while trailingSlash is never and a base path is set Feb 7, 2023
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Thank you! This was probably caused through #8838 (since the issue says it starts erroring on 1.4 or later), but I don't fully understand how. @benmccann @Rich-Harris maybe you can have a closer look as well.

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@Rich-Harris
Copy link
Member

thank you! the actual fix was a bit different, but the failing test was super helpful for diagnosing it

@benmccann benmccann changed the title fix: prerendering error when accessing assets from the root page while trailingSlash is never and a base path is set fix: always default paths.assets to paths.base Feb 8, 2023
@benmccann
Copy link
Member

my bad. I swear I had the code written as Rich has it now because that's how it was before my change. I must have accidentally lost it at some point. glad we have a test for it now! thank you!

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.

4 participants