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: normalize html path in transformIndexHtml() #18976

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vanaigr
Copy link

@vanaigr vanaigr commented Dec 16, 2024

Problem

Currently, the url accepted by transformIndexHtml() is passed unaltered to applyHtmlTransforms().This causes issues if the url

  1. doesn't refer to a file (see SSR dev server "Pre-transform error: Failed to load url" for relative path #18964), or
  2. contains search parameters or hash (/index.html?a=b or index.html#id).

This is different from how default vite server and build behave; they use urls that refer to a file. (The server modifies urls in htmlFallbackMiddleware()).

Solution

I extracted html path normalization from htmlFallbackMiddleware() and used it in createDevHtmlTransformFn().

Fixes #18964

@vanaigr
Copy link
Author

vanaigr commented Dec 16, 2024

Also noticed that SSR guide uses req.originalUrl as the html url.

Not sure if this is known, but it doesn't work with base config option since the base removing middleware doesn't affect originalUrl.

return applyHtmlTransforms(html, transformHooks, {
path: url,
filename: getHtmlFilename(url, server),
path: normalized.url,
Copy link
Member

Choose a reason for hiding this comment

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

I think we shouldn't normalize path here. We don't know if / is responding with the same HTML file with /index.html when appType: 'custom'.
Instead, I think we should make devHtmlHook function work with path ending with /.

const devHtmlHook: IndexHtmlTransformHook = async (

Copy link
Author

Choose a reason for hiding this comment

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

My reasoning was that the path should be the same during build and development. And I assumed that the paths always refer to a file during build.

Can the html path be / during build?

Copy link
Member

Choose a reason for hiding this comment

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

Can the html path be / during build?

No, it always ends with .html during build currently. But it's valid to only call transformIndexHtml in dev and not use it in build.

@sapphi-red sapphi-red added feat: html p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SSR dev server "Pre-transform error: Failed to load url" for relative path
3 participants