Skip to content

fix(html): handle trailing slash in htmlPath for relative URL pre-transform#21429

Open
VedantMadane wants to merge 1 commit intovitejs:mainfrom
VedantMadane:fix/ssr-pre-transform-trailing-slash
Open

fix(html): handle trailing slash in htmlPath for relative URL pre-transform#21429
VedantMadane wants to merge 1 commit intovitejs:mainfrom
VedantMadane:fix/ssr-pre-transform-trailing-slash

Conversation

@VedantMadane
Copy link
Copy Markdown

Description

When implementing an SSR dev server following the SSR guide, the URL passed to transformIndexHtml() may end with a slash (e.g., /example/dir/ instead of /example/dir/index.html).

In this case, path.posix.dirname() returns the parent directory instead of the expected current directory:

  • path.posix.dirname('/example/dir/') returns /example (wrong)
  • Expected: /example/dir

This causes relative script URLs in the HTML to be pre-transformed with the wrong base path, resulting in:

Pre-transform error: Failed to load url /example/filename.js (resolved id: /example/filename.js). Does the file exist?

Fix

When htmlPath ends with /, we treat it as a directory path and remove the trailing slash to get the correct directory, instead of calling dirname().

Related Issue

Fixes #18964

What is the purpose of this pull request?

  • Bug fix

Checklist

…nsform

When htmlPath ends with a slash (e.g., '/example/dir/'), path.posix.dirname()
returns '/example' instead of '/example/dir', causing relative script URLs
to be resolved in the wrong directory.

This can occur when implementing an SSR dev server following the guide,
where the url passed to transformIndexHtml() may end with a slash.

Fixes vitejs#18964
@VedantMadane VedantMadane force-pushed the fix/ssr-pre-transform-trailing-slash branch from 2f1f611 to 0267462 Compare February 10, 2026 12:02
@VedantMadane
Copy link
Copy Markdown
Author

Hi @vitejs, I've updated this PR. Could you please take a look when you have a moment? 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.

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

1 participant