Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions packages/gatsby/cache-dir/__tests__/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,41 @@ describe(`Production loader`, () => {
expect(await prodLoader.loadPageDataJson(`/mypage/`)).toBe(expectation)
expect(xhrCount).toBe(1)
})

it(`should include pathPrefix in HEAD request for missing HTML`, async () => {
global.__BASE_PATH__ = `/blog`
global.__PATH_PREFIX__ = `/blog`

const prodLoader = new ProdLoader(null, [])

const payload = { ...defaultPayload, path: `/404.html/` }
mock.use(
`GET`,
/\/blog\/page-data\/unknown-page\/page-data\.json/,
(req, res) => {
xhrCount++
return res.status(200).body(``)
}
)
mock.use(
`GET`,
/\/blog\/page-data\/404\.html\/page-data\.json/,
(req, res) => {
xhrCount++
res.header(`content-type`, `application/json`)
return res.status(200).body(JSON.stringify(payload))
}
)

let requestedPath
mock.use(`HEAD`, /.*/, (req, res) => {
requestedPath = req.url().path
return res.status(404)
})

await prodLoader.loadPageDataJson(`/unknown-page/`)
expect(requestedPath).toBe(`/blog/unknown-page/`)
})
})

describe(`loadPage`, () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,15 @@ export class ProdLoader extends BaseLoader {
loadPageDataJson(rawPath) {
return super.loadPageDataJson(rawPath).then(data => {
if (data.notFound) {
if (shouldAbortFetch(rawPath)) {
const headPath = rawPath.startsWith(__BASE_PATH__)
? rawPath
: `${__BASE_PATH__}${rawPath}`
if (shouldAbortFetch(headPath)) {
return data
}
// check if html file exist using HEAD request:
// if it does we should navigate to it instead of showing 404
return doFetch(rawPath, `HEAD`).then(req => {
return doFetch(headPath, `HEAD`).then(req => {
if (req.status === 200) {
// page (.html file) actually exist (or we asked for 404 )
// returning page resources status as errored to trigger
Expand All @@ -909,12 +912,15 @@ export class ProdLoader extends BaseLoader {
loadPartialHydrationJson(rawPath) {
return super.loadPartialHydrationJson(rawPath).then(data => {
if (data.notFound) {
if (shouldAbortFetch(rawPath)) {
const headPath = rawPath.startsWith(__BASE_PATH__)
? rawPath
: `${__BASE_PATH__}${rawPath}`
if (shouldAbortFetch(headPath)) {
return data
}
// check if html file exist using HEAD request:
// if it does we should navigate to it instead of showing 404
return doFetch(rawPath, `HEAD`).then(req => {
return doFetch(headPath, `HEAD`).then(req => {
if (req.status === 200) {
// page (.html file) actually exist (or we asked for 404 )
// returning page resources status as errored to trigger
Expand Down
Loading