From 28fd5363696168cd9852f6f376b0a7c2d0cecec5 Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Sun, 12 Nov 2023 00:27:25 +0500 Subject: [PATCH 1/5] add ignore base option --- src/main.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 3f73d19..f5bab2b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,6 +33,7 @@ const Config = { transformer: undefined as | undefined | ((html: string, req: express.Request) => string), + ignoreBase: true as boolean, }; type ConfigurationOptions = Partial; @@ -213,7 +214,7 @@ async function injectViteIndexMiddleware( ) { const config = await getViteConfig(); - app.get("/*", async (req, res, next) => { + app.use(Config.ignoreBase ? "/" : config.base, async (req, res, next) => { if (isIgnoredPath(req.path, req)) return next(); if (isStaticFilePath(req.path)) next(); @@ -230,11 +231,12 @@ async function injectViteIndexMiddleware( async function injectIndexMiddleware(app: core.Express) { const distPath = await getDistPath(); + const config = await getViteConfig(); - app.use("*", (req, res, next) => { - if (isIgnoredPath(req.baseUrl, req)) return next(); + app.use(Config.ignoreBase ? "/" : config.base, (req, res, next) => { + if (isIgnoredPath(req.path, req)) return next(); - const indexPath = findClosestIndexToRoot(req.originalUrl, distPath); + const indexPath = findClosestIndexToRoot(req.path, distPath); if (indexPath === undefined) return next(); const html = fs.readFileSync(indexPath, "utf8"); @@ -266,6 +268,7 @@ function config(config: ConfigurationOptions) { Config.ignorePaths = config.ignorePaths; Config.inlineViteConfig = config.inlineViteConfig; Config.transformer = config.transformer; + if (config.ignoreBase !== undefined) Config.ignoreBase = config.ignoreBase; } async function bind( From bdcbe684f5302520d534abd6d291cc646e8689f3 Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Sun, 12 Nov 2023 20:40:42 +0500 Subject: [PATCH 2/5] Vite Index Middleware should only handle GET requests --- src/main.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.ts b/src/main.ts index f5bab2b..ad73e69 100644 --- a/src/main.ts +++ b/src/main.ts @@ -215,6 +215,8 @@ async function injectViteIndexMiddleware( const config = await getViteConfig(); app.use(Config.ignoreBase ? "/" : config.base, async (req, res, next) => { + if (req.method !== "GET") return next(); + if (isIgnoredPath(req.path, req)) return next(); if (isStaticFilePath(req.path)) next(); From f529b2842f45baa6bea1f9eee51f13b39cd0480c Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Fri, 17 Nov 2023 16:51:03 +0500 Subject: [PATCH 3/5] Use base directly instead of introducing another config option --- src/main.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index ad73e69..5e9d07b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,7 +33,6 @@ const Config = { transformer: undefined as | undefined | ((html: string, req: express.Request) => string), - ignoreBase: true as boolean, }; type ConfigurationOptions = Partial; @@ -214,9 +213,9 @@ async function injectViteIndexMiddleware( ) { const config = await getViteConfig(); - app.use(Config.ignoreBase ? "/" : config.base, async (req, res, next) => { + app.use(config.base, async (req, res, next) => { if (req.method !== "GET") return next(); - + if (isIgnoredPath(req.path, req)) return next(); if (isStaticFilePath(req.path)) next(); @@ -235,7 +234,7 @@ async function injectIndexMiddleware(app: core.Express) { const distPath = await getDistPath(); const config = await getViteConfig(); - app.use(Config.ignoreBase ? "/" : config.base, (req, res, next) => { + app.use(config.base, (req, res, next) => { if (isIgnoredPath(req.path, req)) return next(); const indexPath = findClosestIndexToRoot(req.path, distPath); @@ -270,7 +269,6 @@ function config(config: ConfigurationOptions) { Config.ignorePaths = config.ignorePaths; Config.inlineViteConfig = config.inlineViteConfig; Config.transformer = config.transformer; - if (config.ignoreBase !== undefined) Config.ignoreBase = config.ignoreBase; } async function bind( From 5079964e64611bc20dba8a94a033adb211668fc9 Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Fri, 17 Nov 2023 18:11:49 +0500 Subject: [PATCH 4/5] Specified base must be included in the page address --- tests/templates.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/templates.test.ts b/tests/templates.test.ts index 07ebc2b..69ad9ec 100644 --- a/tests/templates.test.ts +++ b/tests/templates.test.ts @@ -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"); it("test set up"); From a3694829798409284c409bc7e2d88dbe29921774 Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Fri, 17 Nov 2023 20:02:44 +0500 Subject: [PATCH 5/5] Add optional base parameter in the testCase --- tests/templates.test.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/templates.test.ts b/tests/templates.test.ts index 69ad9ec..50a43ea 100644 --- a/tests/templates.test.ts +++ b/tests/templates.test.ts @@ -46,25 +46,24 @@ for (const template of templates) { test(`Template "${template}" with custom inline config`, async (done) => { process.chdir(`create-vite-express/templates/${template}`); + const base = "/admin"; + ViteExpress.config({ - inlineViteConfig: { - base: "/admin", - build: { outDir: "out" }, - }, + inlineViteConfig: { base, build: { outDir: "out" } }, }); await ViteExpress.build(); - await testCase(template, done); + await testCase(template, done, base); }); } -const testCase = async (template: string, done: () => void) => { +const testCase = async (template: string, done: () => void, base = "/") => { const server = ViteExpress.listen(express(), 3000, () => { const browser = puppeteer.launch(); browser.then(async (browser) => { const page = await browser.newPage(); - await page.goto("http://localhost:3000/admin"); + await page.goto(`http://localhost:3000${base}`); it("test set up");