Skip to content

Commit

Permalink
reorder how html files are loaded, don't reach for fallback immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Oct 12, 2020
1 parent d61d7d7 commit 1f476b2
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,34 +446,64 @@ export async function startServer(commandOptions: CommandOptions) {
} else {
continue;
}
if (isRoute) {
let fileLoc =
(await attemptLoadFile(requestedFile)) ||
(await attemptLoadFile(requestedFile + '.html')) ||
(await attemptLoadFile(requestedFile + 'index.html')) ||
(await attemptLoadFile(requestedFile + '/index.html'));

if (!fileLoc && dirUrl === '/' && config.devOptions.fallback) {
const fallbackFile = path.join(dirDisk, config.devOptions.fallback);
fileLoc = await attemptLoadFile(fallbackFile);
}
const fileLocExact = await attemptLoadFile(requestedFile);
if (fileLocExact) {
return fileLocExact;
}
for (const potentialSourceFile of getInputsFromOutput(requestedFile, config.plugins)) {
const fileLoc = await attemptLoadFile(potentialSourceFile);
if (fileLoc) {
responseFileExt = '.html';
return fileLoc;
}
}
}
return null;
}

async function getFileFromRoute(reqPath: string): Promise<string | null> {
for (const [dirDisk, dirUrl] of Object.entries(config.mount)) {
let requestedFile: string;
if (dirUrl === '/') {
requestedFile = path.join(dirDisk, reqPath);
} else if (reqPath.startsWith(dirUrl)) {
requestedFile = path.join(dirDisk, reqPath.replace(dirUrl, './'));
} else {
for (const potentialSourceFile of getInputsFromOutput(requestedFile, config.plugins)) {
const fileLoc = await attemptLoadFile(potentialSourceFile);
if (fileLoc) {
return fileLoc;
}
}
continue;
}
let fileLoc =
(await attemptLoadFile(requestedFile)) ||
(await attemptLoadFile(requestedFile + '.html')) ||
(await attemptLoadFile(requestedFile + 'index.html')) ||
(await attemptLoadFile(requestedFile + '/index.html'));
if (fileLoc) {
responseFileExt = '.html';
return fileLoc;
}
}
return null;
}

const fileLoc = await getFileFromUrl(reqPath);
async function getFileFromFallback(): Promise<string | null> {
for (const [dirDisk, dirUrl] of Object.entries(config.mount)) {
if (dirUrl !== '/') {
continue;
}
if (config.devOptions.fallback) {
const fallbackFile = path.join(dirDisk, config.devOptions.fallback);
fileLoc = await attemptLoadFile(fallbackFile);
}
if (fileLoc) {
responseFileExt = '.html';
return fileLoc;
}
}
return null;
}

let fileLoc = await getFileFromUrl(reqPath);
if (!fileLoc && isRoute) {
fileLoc = (await getFileFromRoute(reqPath)) || (await getFileFromFallback());
}

if (!fileLoc) {
// Don't log favicon "Not Found" errors. Browsers automatically request a favicon.ico file
Expand Down

1 comment on commit 1f476b2

@vercel
Copy link

@vercel vercel bot commented on 1f476b2 Oct 12, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.