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

Explicitly requesting /index.html in production returns untransformed html #109

Closed
mgmeedendorp opened this issue Jan 11, 2024 · 4 comments

Comments

@mgmeedendorp
Copy link

Hi! Thank you for developing this library, it has really simplified transferring my projects to vite :)

I am using the feature to transform the index.html file on-the-fly. When I use vite-express in production mode and I request the /index.html path explicitly, it seems like the index.html file is served from disk as if it were a static file.

I would expect the html transformer function to be called in this case too.

I'm not sure if you see this as a bug or an intended feature, but would like to point out that this issue exists for me.

Currently I have applied a workaround by inserting the following code before calling ViteExpress.listen:

// If we allow direct access to index.html, the user sees a non-transformed version of the index.html page, which is not what we want.
express.get('/index.html', (req, res) => {
    res.redirect('/')
})
@szymmis
Copy link
Owner

szymmis commented Jan 11, 2024

Hi @mgmeedendorp, it looks like this is intended feature because html transformer is used only when handling non-static file requests, so I don't plan to make any changes to it.

May I ask why do you need to handle /index.html explicitly?
Nevertheless it's very nice that you were able to find a quick workaround for that.

@mgmeedendorp
Copy link
Author

mgmeedendorp commented Jan 11, 2024

Hi @szymmis , thanks for the quick reply.

The reason this was a problem for me is very specific to my project so I understand that you don't intend to change anything. I have a workable solution anyways.

The situation is as follows:
I maintain a website which can be installed as a PWA. When I initially added this functionality (a few years ago), I set the start_url field to '/index.html'. This start_url defines which page will be requested when opening the installed app.
There are some users who have installed this old version of the PWA, and changing the start_url seems to require a re-install, which is unfortunate. So after I migrated the site to use vite-express, the users that installed the progressive web app in the past were explicitly requesting the 'index.html' path, and because they were served the unmodified index.html (no javascript tags are injected either) the page was blank.

I understand your reasoning that the html transformer should only be used when handling non-static files, but in that case it would not even make sense in my project to have the 'index.html' file be publicly accessible (in which case requesting index.html would not match any route and serve the transformed html). Is it possible to configure vite-express so that the index.html file is not public?

@szymmis
Copy link
Owner

szymmis commented Jan 15, 2024

If you want to have control over what's served in production mode, one of the solutions I see is invoking ViteExpress.bind only in dev mode, and in production using express static (that's what vite-express is using under the hood in production) or something else but I don't know what kind of control over some file white/blacklisting you would have. You can also make some middleware to redirect as you did or even return 404 on request to /index.html. As I said, it is all up to you as vite-express doesn't any tools to support it right now.

EDIT: Okay, now that I though about it more, I don't want to transform static files and requesting html files explicitly by their name can be interpreted as such, but I do agree that there isn't any value in making this possible and it can be confusing that those requests are not transformed by the transformer function. That's why I think vite-express should do what you are asking about: not serving html files as static files in production mode, just like is not serving them in dev mode.

I'll look into this matter.

@szymmis
Copy link
Owner

szymmis commented Jan 21, 2024

Should be resolved by #110.

@szymmis szymmis closed this as completed Jan 21, 2024
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