Skip to content

Commit 4db2c68

Browse files
ematipicoastrobot-houston
authored andcommitted
[ci] format
1 parent dcb9526 commit 4db2c68

File tree

2 files changed

+112
-97
lines changed

2 files changed

+112
-97
lines changed

packages/astro/src/core/app/index.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ import { AppPipeline } from './pipeline.js';
3131

3232
export { deserializeManifest } from './common.js';
3333

34-
35-
type ErrorPagePath =
36-
| `${string}/404`
37-
| `${string}/500`
38-
| `${string}/404/`
39-
| `${string}/500/`
40-
| `${string}404.html`
41-
| `${string}500.html`;
34+
type ErrorPagePath =
35+
| `${string}/404`
36+
| `${string}/500`
37+
| `${string}/404/`
38+
| `${string}/500/`
39+
| `${string}404.html`
40+
| `${string}500.html`;
4241

4342
export interface RenderOptions {
4443
/**
@@ -348,7 +347,12 @@ export class App {
348347
if (typeof locals !== 'object') {
349348
const error = new AstroError(AstroErrorData.LocalsNotAnObject);
350349
this.#logger.error(null, error.stack!);
351-
return this.#renderError(request, { status: 500, error, clientAddress, prerenderedErrorPageFetch: prerenderedErrorPageFetch, });
350+
return this.#renderError(request, {
351+
status: 500,
352+
error,
353+
clientAddress,
354+
prerenderedErrorPageFetch: prerenderedErrorPageFetch,
355+
});
352356
}
353357
}
354358
if (!routeData) {
@@ -367,7 +371,12 @@ export class App {
367371
if (!routeData) {
368372
this.#logger.debug('router', "Astro hasn't found routes that match " + request.url);
369373
this.#logger.debug('router', "Here's the available routes:\n", this.#manifestData);
370-
return this.#renderError(request, { locals, status: 404, clientAddress, prerenderedErrorPageFetch: prerenderedErrorPageFetch });
374+
return this.#renderError(request, {
375+
locals,
376+
status: 404,
377+
clientAddress,
378+
prerenderedErrorPageFetch: prerenderedErrorPageFetch,
379+
});
371380
}
372381
const pathname = this.#getPathnameFromRequest(request);
373382
const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
@@ -391,7 +400,13 @@ export class App {
391400
response = await renderContext.render(await mod.page());
392401
} catch (err: any) {
393402
this.#logger.error(null, err.stack || err.message || String(err));
394-
return this.#renderError(request, { locals, status: 500, error: err, clientAddress, prerenderedErrorPageFetch: prerenderedErrorPageFetch });
403+
return this.#renderError(request, {
404+
locals,
405+
status: 500,
406+
error: err,
407+
clientAddress,
408+
prerenderedErrorPageFetch: prerenderedErrorPageFetch,
409+
});
395410
} finally {
396411
await session?.[PERSIST_SYMBOL]();
397412
}
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
11
import assert from 'node:assert/strict';
2-
import { before, describe, it, beforeEach } from 'node:test';
2+
import { before, beforeEach, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
44
import testAdapter from './test-adapter.js';
55
import { loadFixture } from './test-utils.js';
66

77
describe('Custom Fetch for Error Pages', () => {
8-
/** @type {import('./test-utils.js').Fixture} */
9-
let fixture;
10-
11-
before(async () => {
12-
fixture = await loadFixture({
13-
root: './fixtures/custom-fetch-error-pages/',
14-
output: 'server',
15-
adapter: testAdapter(),
16-
build: { inlineStylesheets: 'never' },
17-
});
18-
});
19-
20-
describe('Production', () => {
21-
/** @type {import('./test-utils.js').App} */
22-
let app;
23-
24-
// Mock fetch calls for tracking
25-
let fetchCalls = [];
26-
const customFetch = async (url) => {
27-
fetchCalls.push(url);
28-
// Return a custom response to verify our fetch was used
29-
return new Response('<html><body><h1>Custom Fetch Response</h1></body></html>', {
30-
headers: {
31-
'content-type': 'text/html',
32-
},
33-
});
34-
};
35-
36-
before(async () => {
37-
await fixture.build({});
38-
app = await fixture.loadTestAdapterApp();
39-
});
40-
41-
beforeEach(() => {
42-
// Reset fetch calls before each test
43-
fetchCalls = [];
44-
});
45-
46-
it('uses custom fetch implementation in case the server needs to get pre-rendered error 404 page when provided via preRenderedFetch', async () => {
47-
const request = new Request('http://example.com/not-found');
48-
const response = await app.render(request, { prerenderedErrorPageFetch: customFetch });
49-
50-
// Verify the response comes from our custom fetch
51-
assert.equal(response.status, 404);
52-
53-
// Verify our custom fetch was called with the right URL
54-
assert.equal(fetchCalls.length, 1);
55-
assert.ok(fetchCalls[0].includes('/404'));
56-
57-
const html = await response.text();
58-
const $ = cheerio.load(html);
59-
assert.equal($('h1').text(), 'Custom Fetch Response');
60-
});
61-
62-
it('uses custom fetch implementation for 500 errors', async () => {
63-
const request = new Request('http://example.com/causes-error');
64-
const response = await app.render(request, { prerenderedErrorPageFetch: customFetch });
65-
66-
// Verify the response comes from our custom fetch
67-
assert.equal(response.status, 500);
68-
69-
// Verify our custom fetch was called with the right URL
70-
assert.equal(fetchCalls.length, 1);
71-
assert.ok(fetchCalls[0].includes('/500'));
72-
73-
const html = await response.text();
74-
const $ = cheerio.load(html);
75-
assert.equal($('h1').text(), 'Custom Fetch Response');
76-
});
77-
78-
it('falls back to global fetch when preRenderedFetch is not provided', async () => {
79-
const request = new Request('http://example.com/not-found');
80-
const response = await app.render(request);
81-
82-
// Verify our custom fetch was NOT called
83-
assert.equal(fetchCalls.length, 0);
84-
85-
// Response should be the default 404 page
86-
assert.equal(response.status, 404);
87-
const html = await response.text();
88-
const $ = cheerio.load(html);
89-
assert.equal($('h1').text(), 'Example Domain'); // actual fetch requesting example.com and gets that.
90-
});
91-
});
92-
});
8+
/** @type {import('./test-utils.js').Fixture} */
9+
let fixture;
10+
11+
before(async () => {
12+
fixture = await loadFixture({
13+
root: './fixtures/custom-fetch-error-pages/',
14+
output: 'server',
15+
adapter: testAdapter(),
16+
build: { inlineStylesheets: 'never' },
17+
});
18+
});
19+
20+
describe('Production', () => {
21+
/** @type {import('./test-utils.js').App} */
22+
let app;
23+
24+
// Mock fetch calls for tracking
25+
let fetchCalls = [];
26+
const customFetch = async (url) => {
27+
fetchCalls.push(url);
28+
// Return a custom response to verify our fetch was used
29+
return new Response('<html><body><h1>Custom Fetch Response</h1></body></html>', {
30+
headers: {
31+
'content-type': 'text/html',
32+
},
33+
});
34+
};
35+
36+
before(async () => {
37+
await fixture.build({});
38+
app = await fixture.loadTestAdapterApp();
39+
});
40+
41+
beforeEach(() => {
42+
// Reset fetch calls before each test
43+
fetchCalls = [];
44+
});
45+
46+
it('uses custom fetch implementation in case the server needs to get pre-rendered error 404 page when provided via preRenderedFetch', async () => {
47+
const request = new Request('http://example.com/not-found');
48+
const response = await app.render(request, { prerenderedErrorPageFetch: customFetch });
49+
50+
// Verify the response comes from our custom fetch
51+
assert.equal(response.status, 404);
52+
53+
// Verify our custom fetch was called with the right URL
54+
assert.equal(fetchCalls.length, 1);
55+
assert.ok(fetchCalls[0].includes('/404'));
56+
57+
const html = await response.text();
58+
const $ = cheerio.load(html);
59+
assert.equal($('h1').text(), 'Custom Fetch Response');
60+
});
61+
62+
it('uses custom fetch implementation for 500 errors', async () => {
63+
const request = new Request('http://example.com/causes-error');
64+
const response = await app.render(request, { prerenderedErrorPageFetch: customFetch });
65+
66+
// Verify the response comes from our custom fetch
67+
assert.equal(response.status, 500);
68+
69+
// Verify our custom fetch was called with the right URL
70+
assert.equal(fetchCalls.length, 1);
71+
assert.ok(fetchCalls[0].includes('/500'));
72+
73+
const html = await response.text();
74+
const $ = cheerio.load(html);
75+
assert.equal($('h1').text(), 'Custom Fetch Response');
76+
});
77+
78+
it('falls back to global fetch when preRenderedFetch is not provided', async () => {
79+
const request = new Request('http://example.com/not-found');
80+
const response = await app.render(request);
81+
82+
// Verify our custom fetch was NOT called
83+
assert.equal(fetchCalls.length, 0);
84+
85+
// Response should be the default 404 page
86+
assert.equal(response.status, 404);
87+
const html = await response.text();
88+
const $ = cheerio.load(html);
89+
assert.equal($('h1').text(), 'Example Domain'); // actual fetch requesting example.com and gets that.
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)