-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: inline proxies for virtual html in transformIndexHtml #7993
fix: inline proxies for virtual html in transformIndexHtml #7993
Conversation
isSelfAccepting
of undefined
I don't know if this is the correct fix. As you showed in #6859 (comment), the inline @poyoho, we're probably missing an SSR + |
In the repro at #6859 (comment),
And then it fails to add the module to the graph because it can resolve the URL to add the module. I don't know how to solve this. Don't we also have the same issue for inlined JS modules @poyoho? |
I think we should do it in the source. @saurabhdaware I think we should add the test case to let errors be found in CI. may be we can vite/packages/vite/src/node/server/transformRequest.ts Lines 143 to 144 in ac05f25
|
in ssr run inlined JS modules are requested by vite/packages/vite/src/node/server/transformRequest.ts Lines 143 to 144 in ac05f25
IIUC it may be no issues in the inline js module. |
@saurabhdaware and I suggest no make vite For example: vite/packages/playground/ssr-html/server.js Lines 47 to 53 in 2a764af
|
aha understood! This one did fix both the issues (#7992, #6859 (comment) let [url] = req.originalUrl.split('?')
if (url.endsWith('/')) url += 'index.html' Let me pull this branch and see if it is working for my case. I'll see if we can add test for this. Thanks @poyoho 🎉 |
So this didn't fix the issue 2a764af This line itself is failing on unable to resolve error.
|
ccda81b this fixed the issue but it still prints that error in terminal. Only the server started working after this. |
a2a0d4f after this too the page started loading but the terminal was still throwing the error 🤔 |
@@ -44,13 +52,18 @@ async function createServer( | |||
|
|||
app.use('*', async (req, res) => { | |||
try { | |||
let [url] = req.originalUrl.split('?') | |||
if (url.endsWith('/')) url += 'index.html' | |||
const url = req.originalUrl |
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.
Updated this code to be a bit similar to SSR docs example
This one is failing with unresolved path error now
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.
#7993 (comment) this can solve the issue. Is that expected behaviour?
Was wondering what exactly is the solution-
- Update docs to add the solution that @poyoho mentioned
- Or fix the resolve logic and automatically handle index.html paths?
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.
We can update the docs in v3 if we think that is the correct way forward, but this was working before and documented so we should merge a fix in 2.9 that works as it is currently documented:
const url = req.originalUrl
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.
You can watch resolve
plugin to know how vite internal resolveId work. So you can set the package.json>main field to avoid the error.
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.
@poyoho do you see an issue with adding this logic:
let [url] = req.originalUrl.split('?')
if (url.endsWith('/')) url += 'index.html'
to transformIndexHtml
?
Or would it later fail in render
(https://vitejs.dev/guide/ssr.html#setting-up-the-dev-server)
// 4. render the app HTML. This assumes entry-server.js's exported `render`
// function calls appropriate framework SSR APIs,
// e.g. ReactDOMServer.renderToString()
const appHtml = await render(url)
Since we are asking users to add the index.html
by hand, why not add it ourselves when needed so we avoid breaking these cases? Maybe we could emit a warning later saying that in v3 this will not be supported and they have to pass a complete path?
So why do we exactly need filename? and wouldn't this cause issues in SSR where someone is doing this- app.get('/', (req, res) => {
const html = `
<html>
<head>
<style>
body {
color: red;
}
</style>
</head>
<body>
index
</body>
</html>
`
const finalHtml = vite.transformIndexHtml(req.url, html);
res.status(200).end(finalHtml)
}); In this case, there won't be any "filename" right? the HTML is generated on the runtime. How do we handle this in vite? |
my mean is add this before let [url] = req.originalUrl.split('?')
if (url.endsWith('/')) url += 'index.html'
vite.transformIndexHtml(url, `html`) I think |
@brillout maybe you could help us here, would you consider that this isn't supported by Vite? So this isn't a regression? vite.transformIndexHtml('/', template) Several of our playgrounds are doing the resolution to add let [url] = req.originalUrl.split('?')
if (url.endsWith('/')) url += 'index.html' The SSR docs don't include this though https://vitejs.dev/guide/ssr.html#setting-up-the-dev-server, so there may be people passing @poyoho maybe for 2.9, should we detect that Not related to this PR, but the transformation of inlined style tags and inline JS, we don't have an SSR flag in transformIndexHtml, so these are transformed as if we weren't in SSR mode. I think there could be bugs hiding here |
vite-plugin-ssr 0.3 does The only thing vite-plugin-ssr needs from That's why I changed vite-plugin-ssr 0.4 to pass a fake HTML to const fakeHtmlBefore = '<html><head></head><body></body></html>'
// vite-plugin-ssr 0.4 always sets the URL to `/` even if the URL is different. (Vite doesn't
// seem to use the URL for that fake HTML.)
const fakeHtmlAfter = await pageContext._viteDevServer.transformIndexHtml('/', fakeHtml)
const scriptTagsInjectedByVite = extractScriptTags(fakeHtmlAfter)
// Insert script tags into the real HTML
// ... In the context of SSR frameworks, I don't see a use case for inline styles HMR. (Some CSS-in-JS library do inject style tags to the SSR'd HTML but these don't need HMR.) @saurabhdaware What's your motivation for using inline style HMR together with SSR? Why not I don't why Vite wants the URL (I've always wondered that actually). But I don't think it's needed for SSR? Long term, maybe the solution is to have a second hook // Doesn't support inline styles HMR, and therefore doesn't require a URL.
export function transformSsrHtml(
html: string,
// `transformIndexHtml()` also injects the base URL, but vite-plugin-ssr doesn't need it and
// it actually causes problems. That's why vite-plugin-ssr uses this trick of passing a fake
// HTML to Vite. I actually wonder if any SSR framework needs it?
options?: { injectBaseUrl?: boolean }
) {
// ...
} Btw. is there a timeline for Vite 3? I think it would be good if we can have some design discussions. (My biggest pet peeve being that Vite doesn't externalize all SSR dependencies by default :-).) At least I think we should list all design flaws we are aware of and that we all agree on. So that we can, step-by-step, go in the right direction. Knowing that the design was agreed on, makes PR'ing much more pleasant. (I can take a stab at a couple of things but let me release Vike & Stem before :-), it's been quite delayed already 😅). |
Aha this seems like a nice workaround.
I don't really mind not having HMR on inlined styles. We can just treat it as any other change in the html file right? and just reload the page? In my case, I am working on an SSG so the |
Thanks for the explanation, this confirms that we have a regression. We can fix it in v2.9, and then think about
We have an open discussion here: If you want to focus on SSR for v3, you can choose to add a thread there, or start a new discussion and link it to the main issue. About the timeline, once this regression is resolved, we are going to start merging the PRs in the v3 milestone. I think we could stay in beta longer for the major so we have more time for changes, so probably v3 would be released in 40-60 days. |
isSelfAccepting
of undefined// Force calling transformIndexHtml with url === '/', to simulate | ||
// usage by ecosystem that was recommended in the SSR documentation | ||
// as `const url = req.originalUrl` | ||
const html = await vite.transformIndexHtml('/', template) |
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'm forcing '/'
as the url
here as @brillout described he was doing in vite-plugin-ssr.
The PR is now using virtual paths for the proxies when the HTML is virtual. This fixes issues with sourcemaps (I think we may be able to remove the |
|
||
// ensure module in graph after successful load | ||
const mod = await moduleGraph.ensureEntryFromUrl(url, false) | ||
ensureWatchedFile(watcher, mod.file, config.root) | ||
|
||
const result = await server!.pluginContainer.transform(code, url) | ||
const result = await server!.pluginContainer.transform(code, mod.id!) |
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.
The transform
hook expects the resolved id as second parameter
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.
BTW, mod.id
is /path-to/index.html?html-proxy&index=0.css
😀
@@ -190,13 +217,13 @@ const devHtmlHook: IndexHtmlTransformHook = async ( | |||
|
|||
await Promise.all( | |||
styleUrl.map(async ({ start, end, code }, index) => { | |||
const url = filename + `?html-proxy&${index}.css` | |||
const url = `${proxyModulePath}?html-proxy&index=${index}.css` |
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.
The url was missing index=
here, I think it worked because this is only needed as a temporary name but still good to follow the same convention as with JS I think.
It is working for me 🎉 Thanks people 👯 |
It feels like this PR was overloaded with extra stuff not mentioned in the merge commit description? It would've been better to do a separate PR or make sure to update the commit description before merging. 😅 Can someone sum up the stuff that isn't directly related to inlining JS/CSS paths for virtual HTML? Or maybe I'm just misunderstanding the point of this PR (no use case was stated in the PR description, so I'm left to guess what is meant by "inlined JS/CSS for virtual HTML"). |
Description
Fixes #7992
When inlined css is passed through the
vite.transformIndexHtml
it was failing withisSelfAccepting
of undefined error on import-analysis plugin.I added an early null return there which seem to fix the issue. Although I am new to Vite's codebase so I am not sure if this is the right fix.
Let me know if something else is a fix.
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).