[ci] format

This commit is contained in:
ematipico 2023-08-17 08:37:38 +00:00 committed by astrobot-houston
parent dff0f0f8dd
commit 2145960472
3 changed files with 34 additions and 31 deletions

View file

@ -286,14 +286,17 @@ export class App {
const errorRouteData = matchRoute('/' + status, this.#manifestData); const errorRouteData = matchRoute('/' + status, this.#manifestData);
const url = new URL(request.url); const url = new URL(request.url);
if (errorRouteData) { if (errorRouteData) {
if (errorRouteData.prerender){ if (errorRouteData.prerender) {
const maybeDotHtml = errorRouteData.route.endsWith(`/${status}`) ? '.html' : '' const maybeDotHtml = errorRouteData.route.endsWith(`/${status}`) ? '.html' : '';
const statusURL = new URL(`${this.#baseWithoutTrailingSlash}/${status}${maybeDotHtml}`, url); const statusURL = new URL(
`${this.#baseWithoutTrailingSlash}/${status}${maybeDotHtml}`,
url
);
const response = await fetch(statusURL.toString()); const response = await fetch(statusURL.toString());
// response for /404.html and 500.html is 200, which is not meaningful // response for /404.html and 500.html is 200, which is not meaningful
// so we create an override // so we create an override
const override = { status } const override = { status };
return this.#mergeResponses(response, originalResponse, override); return this.#mergeResponses(response, originalResponse, override);
} }
@ -322,27 +325,28 @@ export class App {
return response; return response;
} }
#mergeResponses(newResponse: Response, oldResponse?: Response, override?: { status : 404 | 500 }) { #mergeResponses(newResponse: Response, oldResponse?: Response, override?: { status: 404 | 500 }) {
if (!oldResponse) { if (!oldResponse) {
if (override !== undefined) { if (override !== undefined) {
return new Response(newResponse.body, { return new Response(newResponse.body, {
status: override.status, status: override.status,
statusText: newResponse.statusText, statusText: newResponse.statusText,
headers: newResponse.headers headers: newResponse.headers,
}) });
} }
return newResponse; return newResponse;
} }
const { statusText, headers } = oldResponse; const { statusText, headers } = oldResponse;
// If the the new response did not have a meaningful status, an override may have been provided // If the the new response did not have a meaningful status, an override may have been provided
// If the original status was 200 (default), override it with the new status (probably 404 or 500) // If the original status was 200 (default), override it with the new status (probably 404 or 500)
// Otherwise, the user set a specific status while rendering and we should respect that one // Otherwise, the user set a specific status while rendering and we should respect that one
const status = const status = override?.status
override?.status ? override.status : ? override.status
oldResponse.status === 200 ? newResponse.status : : oldResponse.status === 200
oldResponse.status ? newResponse.status
: oldResponse.status;
return new Response(newResponse.body, { return new Response(newResponse.body, {
status, status,

View file

@ -341,7 +341,7 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
// Replace exports (only prerendered pages) with a noop // Replace exports (only prerendered pages) with a noop
let value = 'const noop = () => {};'; let value = 'const noop = () => {};';
for (const e of exports) { for (const e of exports) {
if (e.n === 'default') value += `\n export default noop;` if (e.n === 'default') value += `\n export default noop;`;
else value += `\nexport const ${e.n} = noop;`; else value += `\nexport const ${e.n} = noop;`;
} }
await fs.promises.writeFile(url, value, { encoding: 'utf8' }); await fs.promises.writeFile(url, value, { encoding: 'utf8' });

View file

@ -61,18 +61,18 @@ describe('Prerender 404', () => {
const res1 = await fetch(url); const res1 = await fetch(url);
const res2 = await fetch(url); const res2 = await fetch(url);
const res3 = await fetch(url); const res3 = await fetch(url);
expect(res1.status).to.equal(404); expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404); expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404); expect(res3.status).to.equal(404);
const html1 = await res1.text(); const html1 = await res1.text();
const html2 = await res2.text(); const html2 = await res2.text();
const html3 = await res3.text(); const html3 = await res3.text();
expect(html1).to.equal(html2); expect(html1).to.equal(html2);
expect(html2).to.equal(html3); expect(html2).to.equal(html3);
const $ = cheerio.load(html1); const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist'); expect($('body').text()).to.equal('Page does not exist');
@ -90,7 +90,7 @@ describe('Prerender 404', () => {
const html2 = await response2.text(); const html2 = await response2.text();
const html3 = await response3.text(); const html3 = await response3.text();
expect(html1).to.contain("Something went wrong"); expect(html1).to.contain('Something went wrong');
expect(html1).to.equal(html2); expect(html1).to.equal(html2);
expect(html2).to.equal(html3); expect(html2).to.equal(html3);
@ -100,11 +100,10 @@ describe('Prerender 404', () => {
const response = await fetch(`http://${server.host}:${server.port}/some-base/fivehundred`); const response = await fetch(`http://${server.host}:${server.port}/some-base/fivehundred`);
const html = await response.text(); const html = await response.text();
const $ = cheerio.load(html); const $ = cheerio.load(html);
// length will be 0 if the stylesheet does not get included // length will be 0 if the stylesheet does not get included
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1); expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
}); });
}); });
describe('Without base', async () => { describe('Without base', async () => {
@ -143,22 +142,22 @@ describe('Prerender 404', () => {
}); });
it('Can handle prerendered 404', async () => { it('Can handle prerendered 404', async () => {
const url = `http://${server.host}:${server.port}/some-base/missing` const url = `http://${server.host}:${server.port}/some-base/missing`;
const res1 = await fetch(url); const res1 = await fetch(url);
const res2 = await fetch(url); const res2 = await fetch(url);
const res3 = await fetch(url); const res3 = await fetch(url);
expect(res1.status).to.equal(404); expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404); expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404); expect(res3.status).to.equal(404);
const html1 = await res1.text(); const html1 = await res1.text();
const html2 = await res2.text(); const html2 = await res2.text();
const html3 = await res3.text(); const html3 = await res3.text();
expect(html1).to.equal(html2); expect(html1).to.equal(html2);
expect(html2).to.equal(html3); expect(html2).to.equal(html3);
const $ = cheerio.load(html1); const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist'); expect($('body').text()).to.equal('Page does not exist');
@ -207,22 +206,22 @@ describe('Hybrid 404', () => {
}); });
it('Can handle prerendered 404', async () => { it('Can handle prerendered 404', async () => {
const url = `http://${server.host}:${server.port}/some-base/missing` const url = `http://${server.host}:${server.port}/some-base/missing`;
const res1 = await fetch(url); const res1 = await fetch(url);
const res2 = await fetch(url); const res2 = await fetch(url);
const res3 = await fetch(url); const res3 = await fetch(url);
expect(res1.status).to.equal(404); expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404); expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404); expect(res3.status).to.equal(404);
const html1 = await res1.text(); const html1 = await res1.text();
const html2 = await res2.text(); const html2 = await res2.text();
const html3 = await res3.text(); const html3 = await res3.text();
expect(html1).to.equal(html2); expect(html1).to.equal(html2);
expect(html2).to.equal(html3); expect(html2).to.equal(html3);
const $ = cheerio.load(html1); const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist'); expect($('body').text()).to.equal('Page does not exist');
@ -264,22 +263,22 @@ describe('Hybrid 404', () => {
}); });
it('Can handle prerendered 404', async () => { it('Can handle prerendered 404', async () => {
const url = `http://${server.host}:${server.port}/missing` const url = `http://${server.host}:${server.port}/missing`;
const res1 = await fetch(url); const res1 = await fetch(url);
const res2 = await fetch(url); const res2 = await fetch(url);
const res3 = await fetch(url); const res3 = await fetch(url);
expect(res1.status).to.equal(404); expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404); expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404); expect(res3.status).to.equal(404);
const html1 = await res1.text(); const html1 = await res1.text();
const html2 = await res2.text(); const html2 = await res2.text();
const html3 = await res3.text(); const html3 = await res3.text();
expect(html1).to.equal(html2); expect(html1).to.equal(html2);
expect(html2).to.equal(html3); expect(html2).to.equal(html3);
const $ = cheerio.load(html1); const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist'); expect($('body').text()).to.equal('Page does not exist');