-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
vanaigr
wants to merge
2
commits into
vitejs:main
Choose a base branch
from
vanaigr:fix-node-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−44
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/vite/src/node/server/__tests__/transformIndexHtml.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { join } from 'node:path' | ||
import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
import { normalizePath } from '../../utils' | ||
import { type ViteDevServer, createServer } from '../index' | ||
|
||
describe('transformIndexHtml', () => { | ||
const root = join(__dirname, 'fixtures/transformIndexHtml') | ||
let server: ViteDevServer | undefined | ||
|
||
let resolveResult: { path: string; filename: string } | undefined | ||
|
||
beforeEach(async () => { | ||
server = await createServer({ | ||
root, | ||
plugins: [ | ||
{ | ||
name: 'transformIndexHtml-record-result', | ||
transformIndexHtml: { | ||
order: 'pre', | ||
handler: (html, ctx) => { | ||
expect(html).toBe('') | ||
expect(resolveResult).toBe(undefined) | ||
resolveResult = { path: ctx.path, filename: ctx.filename } | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
}) | ||
afterEach(async () => { | ||
if (server) { | ||
await server.close() | ||
server = undefined | ||
} | ||
}) | ||
|
||
async function test(url: string, expPath: string, expFilename: string) { | ||
resolveResult = undefined | ||
await server!.transformIndexHtml(url, '') | ||
expect(resolveResult).toEqual({ | ||
path: expPath, | ||
filename: normalizePath(expFilename), | ||
}) | ||
} | ||
|
||
const p = join(root, 'a', 'b') | ||
|
||
it('handles root url', async () => { | ||
await test('/', '/index.html', join(root, 'index.html')) | ||
}) | ||
|
||
it('handles filename in a subdirectory', async () => { | ||
await test('/a/b/index.html', '/a/b/index.html', join(p, 'index.html')) | ||
}) | ||
it('handles no filename in a subdirectory', async () => { | ||
await test('/a/b/', '/a/b/index.html', join(p, 'index.html')) | ||
}) | ||
it('handles filename without an extension', async () => { | ||
await test('/a/b/index', '/a/b/index.html', join(p, 'index.html')) | ||
}) | ||
|
||
it('handles filename other than index.html without extension', async () => { | ||
await test('/a/b/other', '/a/b/other.html', join(p, 'other.html')) | ||
}) | ||
it('handles unicode in the path', async () => { | ||
await test( | ||
'/%F0%9F%90%B1/', | ||
'/%F0%9F%90%B1/index.html', | ||
join(root, '🐱', 'index.html'), | ||
) | ||
}) | ||
|
||
it('handles search parameters', async () => { | ||
await test('/a/b/?c=d', '/a/b/index.html', join(p, 'index.html')) | ||
}) | ||
|
||
it('handles hash', async () => { | ||
await test('/a/b/#c', '/a/b/index.html', join(p, 'index.html')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
whenappType: 'custom'
.Instead, I think we should make
devHtmlHook
function work withpath
ending with/
.vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Line 185 in 3734f80
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it always ends with
.html
during build currently. But it's valid to only calltransformIndexHtml
in dev and not use it in build.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, if no constraints are placed on the path, shouldn't the user decide filename as well?
What if there would be 2 versions of the function, and one of them would work like the builtin vite server but allow using SSR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was thinking of adding
file
parameter toserver.transformIndexHtml
. Something like:But this requires more code changes. It would be awesome if you want to work on this (no pressure, I'm also fine only with the fix).