szymmis/vite-express

Explicitly requesting /index.html in production returns untransformed html

Closed this issue · 4 comments

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('/')
})

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.

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?

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.

Should be resolved by #110.