diff --git a/src/build.ts b/src/build.ts index aef96b9184..f746ceb263 100644 --- a/src/build.ts +++ b/src/build.ts @@ -45,8 +45,8 @@ async function writeFilep(outPath: URL, bytes: string | Buffer, encoding: 'utf-8 /** Utility for writing a build result to disk */ async function writeResult(result: LoadResult, outPath: URL, encoding: null | 'utf-8') { if (result.statusCode === 500 || result.statusCode === 404) { - error(logging, 'build', result.error || result.statusCode); - } else if(result.statusCode !== 200) { + error(logging, 'build', result.error || result.statusCode); + } else if (result.statusCode !== 200) { error(logging, 'build', `Unexpected load result (${result.statusCode}) for ${outPath.pathname}`); } else { const bytes = result.contents; diff --git a/src/runtime.ts b/src/runtime.ts index 62bbdb09c1..800c4d40a1 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -26,7 +26,7 @@ type LoadResultSuccess = { contentType?: string | false; }; type LoadResultNotFound = { statusCode: 404; error: Error }; -type LoadResultRedirect = { statusCode: 301 | 302; location: string; }; +type LoadResultRedirect = { statusCode: 301 | 302; location: string }; type LoadResultError = { statusCode: 500 } & ({ type: 'parse-error'; error: CompileError } | { type: 'unknown'; error: Error }); export type LoadResult = LoadResultSuccess | LoadResultNotFound | LoadResultRedirect | LoadResultError; @@ -45,7 +45,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro info(logging, 'access', reqPath); const searchResult = searchForPage(fullurl, astroRoot); - if(searchResult.statusCode === 404) { + if (searchResult.statusCode === 404) { try { const result = await frontendSnowpack.loadUrl(reqPath); @@ -65,7 +65,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro } } - if(searchResult.statusCode === 301) { + if (searchResult.statusCode === 301) { return { statusCode: 301, location: searchResult.pathname }; } diff --git a/src/search.ts b/src/search.ts index d9e2fa00ce..043b5c347e 100644 --- a/src/search.ts +++ b/src/search.ts @@ -6,54 +6,57 @@ interface PageLocation { } function findAnyPage(candidates: Array, astroRoot: URL): PageLocation | false { - for(let candidate of candidates) { + for (let candidate of candidates) { const url = new URL(`./pages/${candidate}`, astroRoot); - if(existsSync(url)) { + if (existsSync(url)) { return { fileURL: url, - snowpackURL: `/_astro/pages/${candidate}.js` + snowpackURL: `/_astro/pages/${candidate}.js`, }; } } return false; } -type SearchResult = { - statusCode: 200; - location: PageLocation; - pathname: string; -} | { - statusCode: 301; - location: null; - pathname: string; -} | { - statusCode: 404; -}; +type SearchResult = + | { + statusCode: 200; + location: PageLocation; + pathname: string; + } + | { + statusCode: 301; + location: null; + pathname: string; + } + | { + statusCode: 404; + }; export function searchForPage(url: URL, astroRoot: URL): SearchResult { const reqPath = decodeURI(url.pathname); const base = reqPath.substr(1); // Try to find index.astro/md paths - if(reqPath.endsWith('/')) { + if (reqPath.endsWith('/')) { const candidates = [`${base}index.astro`, `${base}index.md`]; const location = findAnyPage(candidates, astroRoot); - if(location) { + if (location) { return { statusCode: 200, location, - pathname: reqPath + pathname: reqPath, }; } } else { // Try to find the page by its name. const candidates = [`${base}.astro`, `${base}.md`]; let location = findAnyPage(candidates, astroRoot); - if(location) { + if (location) { return { statusCode: 200, location, - pathname: reqPath + pathname: reqPath, }; } } @@ -61,15 +64,15 @@ export function searchForPage(url: URL, astroRoot: URL): SearchResult { // Try to find name/index.astro/md const candidates = [`${base}/index.astro`, `${base}/index.md`]; const location = findAnyPage(candidates, astroRoot); - if(location) { + if (location) { return { statusCode: 301, location: null, - pathname: reqPath + '/' + pathname: reqPath + '/', }; } return { - statusCode: 404 + statusCode: 404, }; -} \ No newline at end of file +} diff --git a/test/astro-basic.test.js b/test/astro-basic.test.js index 053bf2fbb0..0a04ca2649 100644 --- a/test/astro-basic.test.js +++ b/test/astro-basic.test.js @@ -16,4 +16,4 @@ Basics('Can load page', async ({ runtime }) => { assert.equal($('h1').text(), 'Hello world!'); }); -Basics.run(); \ No newline at end of file +Basics.run();