donnikitos / vite-plugin-php

Vite's speed and tooling to preprocess PHP-files!
https://www.npmjs.com/package/vite-plugin-php
MIT License
30 stars 0 forks source link

Vite not serving the PHP file in routes other than / #17

Closed andvaranaut closed 2 months ago

andvaranaut commented 2 months ago

Hi,

first of all, many thanks for your work!

I have run across a small issue while using the plugin for a Vue app. I have been able to install and run it without issue, and the index.php file is correctly served when opening the application root.

If you then navigate to a different route (eg. /login) it works fine, as long as the navigation is handled internally by the Vue router.

However, if you directly open a different route (eg. /login), or just press F5 to reload the page, the PHP file is not served. Vite returns either the contents of index.html (if present) or an empty page (otherwise).

Would it be possible to have the plugin serve index.php regardless of the entry route while in development?

donnikitos commented 2 months ago

Hi @andvaranaut, thanks for for your submission. I am glad the plugin works as expected and found it's userbase 😁 Looking forward to grow it into something even bigger and attract even more users.

Such a router is actually already in the works and should be ready somewhere by the end of the week.

andvaranaut commented 2 months ago

Hi @donnikitos,

that's great to hear! As a workaround, I have just used a static index.html as a fallback. And, of course, it works flawlessly in production as long as you redirect all requests to index.php (instead of index.html) - just like in any other app.

If I may make a suggestion, and as long as it's feasible, it would be great if you could mock $_SERVER['REQUEST_URI'] also :-) (In my use case, I'm using the plugin to change the OpenGraph tags served depending on the route).

donnikitos commented 2 months ago

mock $_SERVER['REQUEST_URI']

I think that is actually a pretty good suggestion, considering the routing would be taken over.

donnikitos commented 2 months ago

@andvaranaut check out the rewriteUrl property, that should help with fixing the routing issue ;)

PS: I would be really glad about a star on the repo 😃

andvaranaut commented 2 months ago

Hi @donnikitos

You have my star! :)

I have made a few tests and it seems to work perfectly in my use case :) Thanks!

If it is any help, I have found that returning undefined in any route that includes a dot or a colon (ie. changing the condition in your example to if (['.', ':'].some((s) => requestUrl.pathname.includes(s)))) is enough to make it work. As long as you don't have routes with any of these characters (or assets without them) it should work fine. :) (The dot filters out any request for a file with an extension, and the colon must be used because of what seem to be some of the Vite hot reload requests).

donnikitos commented 1 month ago

Awesome!

some of the Vite hot reload requests

I actually tried to exclude those automatically, do you maybe know exactly which ones those are?