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

Serving inline scripts in dev mode #158

Open
timswanson-google opened this issue Feb 5, 2025 · 2 comments
Open

Serving inline scripts in dev mode #158

timswanson-google opened this issue Feb 5, 2025 · 2 comments

Comments

@timswanson-google
Copy link

timswanson-google commented Feb 5, 2025

Hello. We are a team at Google that found Vite-Express from its listing here in Awesome-Vite:

https://github.com/vitejs/awesome-vite?tab=readme-ov-file#nodejs

(This page is linked from Vite's docs on Backend Integrations)

We are evaluating it for use in our project, and have come across an issue that we could use your help on.

It seems that Vite-Express does not serve inline JS modules (i.e. <script type="module"> tags in HTML) in dev mode. I'm wondering if there's a configuration or setup step that we're missing.


I've created a minimal example application here, with a branch that demonstrates the issue.

https://github.com/timswanson-google/vite-express-example/tree/inline

In this example, the index.html file contains two scripts. One is loaded via a src attribute, and the other has its content directly inline.

https://github.com/timswanson-google/vite-express-example/blob/4b93508b3ac238172147f79f7ab64cfcc4424545/index.html#L11-L14

When running the server in production mode, everything works fine. The JS is compiled and executes on load.


However, when running in dev mode, only the first module gets executed. The inline script never does.

Specifically, when the index.html page is served, the script tag is rewritten to look like this:

<script type="module" src="/@id/__x00__/home/timswanson/vite-express-example/index.html?html-proxy&index=0.js"></script>

(Where /home/timswanson/vite-express-example is the path to the repo.)

This is the mechanism that the Vite Dev Server uses to package inline JS modules. However, when ViteExpress handles this request, it interprets the request as a static-asset request, and serves index.html again.

This creates the following error in the browser console:

Failed to load module script: Expected a JavaScript module script
but the server responded with a MIME type of "text/html".
Strict MIME type checking is enforced for module scripts per HTML spec.

Is there something that needs to be done to get the ViteExpress handler to pass these requests to the Vite Dev Server?

Thank you.

@timswanson-google
Copy link
Author

Note that <script type="module"> is necessary to trigger this issue. Non-module code is not transformed by Vite, and is served literally, so it functions as expected.

@dglazkov
Copy link

dglazkov commented Feb 5, 2025

I wonder if it's something as simple as adding the check for ?html-proxy in url here:

vite-express/src/main.ts

Lines 226 to 227 in 694face

function findTemplateFilePath(
reqPath: string,

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

No branches or pull requests

2 participants