Skip to content

Commit

Permalink
Update to leverage response-cache for image-optimizer (#34075)
Browse files Browse the repository at this point in the history
This updates to leverage our `response-cache` used for ISR for our `image-optimizer` as well. 

Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
ijjk and styfle committed Feb 8, 2022
1 parent 3f0a8d9 commit 130f864
Show file tree
Hide file tree
Showing 15 changed files with 2,030 additions and 1,569 deletions.
24 changes: 19 additions & 5 deletions packages/next/server/api-utils.ts
Expand Up @@ -365,15 +365,29 @@ async function unstable_revalidate(
)
}

if (typeof urlPath !== 'string' || !urlPath.startsWith('/')) {
throw new Error(
`Invalid urlPath provided to revalidate(), must be a path e.g. /blog/post-1, received ${urlPath}`
)
}

const baseUrl = context.trustHostHeader
? `https://${req.headers.host}`
: `http://${context.hostname}:${context.port}`

return fetch(`${baseUrl}${urlPath}`, {
headers: {
[PRERENDER_REVALIDATE_HEADER]: context.previewModeId,
},
})
try {
const res = await fetch(`${baseUrl}${urlPath}`, {
headers: {
[PRERENDER_REVALIDATE_HEADER]: context.previewModeId,
},
})

if (!res.ok) {
throw new Error(`Invalid response ${res.status}`)
}
} catch (err) {
throw new Error(`Failed to revalidate ${urlPath}`)
}
}

const COOKIE_NAME_PRERENDER_BYPASS = `__prerender_bypass`
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/base-server.ts
Expand Up @@ -1545,6 +1545,8 @@ export default abstract class Server {
await handleRedirect(cachedData.props)
return null
}
} else if (cachedData.kind === 'IMAGE') {
throw new Error('invariant SSG should not return an image cache value')
} else {
return {
type: isDataReq ? 'json' : 'html',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/config-shared.ts
Expand Up @@ -8,7 +8,7 @@ import {
} from './image-config'

export type NextConfigComplete = Required<NextConfig> & {
images: ImageConfigComplete
images: Required<ImageConfigComplete>
typescript: Required<TypeScriptConfig>
configOrigin?: string
configFile?: string
Expand Down

0 comments on commit 130f864

Please sign in to comment.