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
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Config = {
transformer: undefined as
| undefined
| ((html: string, req: express.Request) => string),
ignoreBase: true as boolean,
};

type ConfigurationOptions = Partial<typeof Config>;
Expand Down Expand Up @@ -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.

if (req.method !== "GET") return next();

if (isIgnoredPath(req.path, req)) return next();

if (isStaticFilePath(req.path)) next();
Expand All @@ -230,11 +233,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");
Expand Down Expand Up @@ -266,6 +270,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(
Expand Down