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

Mount HTML serving middlewares at config.base #91

Merged
merged 5 commits into from
Nov 17, 2023
Merged

Mount HTML serving middlewares at config.base #91

merged 5 commits into from
Nov 17, 2023

Conversation

rmhaiderali
Copy link
Contributor

@rmhaiderali rmhaiderali commented Nov 11, 2023

IndexMiddleware and ViteIndexMiddleware currently do not take the base into account and always mount at "/".

Providing developers with the option to decide between mounting both index middlewares at the root or a specified base would enhance flexibility in configuration.

@rmhaiderali rmhaiderali changed the title Make skipping the base optional Make skipping "vite base" optional Nov 12, 2023
@szymmis
Copy link
Owner

szymmis commented Nov 16, 2023

Hi @haider53e, sorry for such a late response.
First of all, thanks a lot for your contribution!

Could you elaborate a little bit more on the problem you are trying to solve? I want to know about the context in which you stumbled upon this problem, as this will let me understand better why current behaviour is incorrect.

@rmhaiderali
Copy link
Contributor Author

Hi @szymmis,

No worries about the delay, and I appreciate your response! The primary objective of this pull request is to align with how the Vite development server mounts applications. Currently, when the base is specified in Vite, it mounts the application at the specified base. However, Vite Express, in its current state, skips the base, and this pull request aims to provide consistency by allowing users to decide whether to skip or include the base.

For clarification, if the base is specified:
base: "/my-subdirectory/"

In Vite: The application is mounted at the specified base.
In Vite Express (before this PR): The application is mounted at the root ("/"), irrespective of the specified base.
This inconsistency can be confusing for developers, and by introducing the option to decide whether to skip or include the base in Vite Express, we align its behavior with Vite, providing a more cohesive experience.

Here's a simple example to illustrate the impact:

Without this PR, Vite Express would mount the application at http://localhost:3000/, whereas with this PR, it can be configured to mount at http://localhost:3000/my-subdirectory/

Copy link
Owner

@szymmis szymmis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for clarification, now I see the issue.

There is a config.base value but it is currently used only in the injectStaticMiddleware function. I think we can just use it in the both functions that you modified instead of introducing another config option, as I said in the comment.

Also, great catch with the req.method.

src/main.ts Outdated
@@ -213,7 +214,9 @@ async function injectViteIndexMiddleware(
) {
const config = await getViteConfig();

app.get("/*", async (req, res, next) => {
app.use(Config.ignoreBase ? "/" : config.base, async (req, res, next) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
app.use(Config.ignoreBase ? "/" : config.base, async (req, res, next) => {
app.use(config.base, async (req, res, next) => {

I think we can use config.base directly without introducing another value as it might be confusing. config.base was added to align vite-express with how Vite works with it, but as you noticed, it isn't used here, only in injectStaticMiddleware function. Also it's / by default that's why we dont need to check if its defined or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, using base directly in both functions will be more concise and align Vite Express with Vite.

@@ -64,7 +64,7 @@ const testCase = async (template: string, done: () => void) => {

browser.then(async (browser) => {
const page = await browser.newPage();
await page.goto("http://localhost:3000");
await page.goto("http://localhost:3000/admin");
Copy link
Owner

@szymmis szymmis Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost got there 😅, what I suggest is to add base parameter to testCase function, that's set to / by default.

const testCase = async (template: string, done: () => void, base = "/") => {
Suggested change
await page.goto("http://localhost:3000/admin");
await page.goto(`http://localhost:3000${base}`);

That's because right now other test would fail (Well, maybe they wouldn't fail because if vite-express is mounted at root, it will serve the page at /admin, but it will definitely make things unclear). If you want to run them locally you can use npm run test or yarn test or any other package manager.

By specifying the parameter as default, you don't need to adjust other testCase invocations, only the one in Template "${template}" with custom inline config test here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made modifications to the code, but on windows, even after attempting to install with both npm and yarn, I consistently encounter the following error when running npm run test with or without administrative privileges:

TEST  Template "react"
FAIL  Error: EACCES: permission denied, scandir 'D:\vite-express\create-vite-express\templates\react\node_modules\.bin\browserslist'
FAIL  18 failed, 0 passed in 0.047706s

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting, I'm running linux so that's why I didn't catch it, but thanks for bringing that up. You can push the code so I can take a look and run the CI if everything looks alright.

@szymmis szymmis changed the title Make skipping "vite base" optional Mount HTML serving middlewares at config.base Nov 17, 2023
Copy link
Owner

@szymmis szymmis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks a lot for your contribution! 🎉
I see that tests are passing so I'll proceed and merge it and make a v0.11.1 release.

@szymmis szymmis merged commit 906c3fa into szymmis:master Nov 17, 2023
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants