Skip to content

Commit

Permalink
Mount HTML serving middlewares at config.base (#91)
Browse files Browse the repository at this point in the history
* add ignore base option
* Vite Index Middleware should only handle GET requests
* Use base directly instead of introducing another config option
* Specified base must be included in the page address
* Add optional base parameter in the testCase
  • Loading branch information
rmhaiderali authored Nov 17, 2023
1 parent 1aa23a3 commit 906c3fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ async function injectViteIndexMiddleware(
) {
const config = await getViteConfig();

app.get("/*", 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();
Expand All @@ -230,11 +232,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.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");
Expand Down
13 changes: 6 additions & 7 deletions tests/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
await page.goto(`http://localhost:3000${base}`);

it("test set up");

Expand Down

0 comments on commit 906c3fa

Please sign in to comment.