Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssg): introduce disableSSG and onlySSG #2104

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { bufferToString } from '../../utils/buffer.ts'
import { getExtension } from '../../utils/mime.ts'
import { joinPaths, dirname } from './utils.ts'

export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'
export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'

/**
* @experimental
* `FileSystemModule` is an experimental feature.
Expand Down Expand Up @@ -128,6 +131,7 @@ export const fetchRoutesContent = async <
const thisRouteBaseURL = new URL(route.path, baseURL).toString()

let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest
forGetInfoURLRequest.headers.set(X_HONO_SSG_HEADER_KEY, 'true')
if (beforeRequestHook) {
const maybeRequest = beforeRequestHook(forGetInfoURLRequest)
if (!maybeRequest) {
Expand All @@ -147,6 +151,9 @@ export const fetchRoutesContent = async <
for (const param of forGetInfoURLRequest.ssgParams) {
const replacedUrlParam = replaceUrlParam(route.path, param)
let response = await app.request(replacedUrlParam, forGetInfoURLRequest)
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) {
continue
}
if (afterResponseHook) {
const maybeResponse = afterResponseHook(response)
if (!maybeResponse) {
Expand Down Expand Up @@ -251,3 +258,29 @@ export const toSSG: ToSSGInterface = async (app, fs, options) => {
await options?.afterGenerateHook?.(result)
return result
}

/**
* @experimental
* `disableSSG` is an experimental feature.
* The API might be changed.
*/

export const disableSSG = (): MiddlewareHandler =>
async function disableSSG(c, next) {
await next()
c.header(X_HONO_DISABLE_SSG_HEADER_KEY, 'true')
}

/**
* @experimental
* `onlySSG` is an experimental feature.
* The API might be changed.
*/
export const onlySSG = (): MiddlewareHandler =>
async function onlySSG(c, next) {
const headerValue = c.req.raw.headers.get(X_HONO_SSG_HEADER_KEY)
if (headerValue) {
await next()
}
return c.notFound()
}
28 changes: 27 additions & 1 deletion src/helper/ssg/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { Hono } from '../../hono'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jsx } from '../../jsx'
import { poweredBy } from '../../middleware/powered-by'
import { fetchRoutesContent, saveContentToFiles, ssgParams, toSSG } from './index'
import {
fetchRoutesContent,
saveContentToFiles,
ssgParams,
toSSG,
disableSSG,
onlySSG,
} from './index'
import type {
BeforeRequestHook,
AfterResponseHook,
Expand Down Expand Up @@ -321,3 +328,22 @@ describe('Dynamic route handling', () => {
expect(htmlMap.has('/foo:bar')).toBeTruthy()
})
})

describe('disableSSG/onlySSG middlewares', () => {
const app = new Hono()
app.get('/', (c) => c.html(<h1>Hello</h1>))
app.get('/api', disableSSG(), (c) => c.text('an-api'))
app.get('/static-page', onlySSG(), (c) => c.html(<h1>Welcome to my site</h1>))

it('Should not generate the page if disableSSG is set', async () => {
const htmlMap = await fetchRoutesContent(app)
expect(htmlMap.has('/')).toBe(true)
expect(htmlMap.has('/static-page')).toBe(true)
expect(htmlMap.has('/api')).toBe(false)
})

it('Should return 404 response if onlySSG() is set', async () => {
const res = await app.request('/static-page')
expect(res.status).toBe(404)
})
})
33 changes: 33 additions & 0 deletions src/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { bufferToString } from '../../utils/buffer'
import { getExtension } from '../../utils/mime'
import { joinPaths, dirname } from './utils'

export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'
export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'

/**
* @experimental
* `FileSystemModule` is an experimental feature.
Expand Down Expand Up @@ -128,6 +131,7 @@ export const fetchRoutesContent = async <
const thisRouteBaseURL = new URL(route.path, baseURL).toString()

let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest
forGetInfoURLRequest.headers.set(X_HONO_SSG_HEADER_KEY, 'true')
if (beforeRequestHook) {
const maybeRequest = beforeRequestHook(forGetInfoURLRequest)
if (!maybeRequest) {
Expand All @@ -147,6 +151,9 @@ export const fetchRoutesContent = async <
for (const param of forGetInfoURLRequest.ssgParams) {
const replacedUrlParam = replaceUrlParam(route.path, param)
let response = await app.request(replacedUrlParam, forGetInfoURLRequest)
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) {
continue
}
if (afterResponseHook) {
const maybeResponse = afterResponseHook(response)
if (!maybeResponse) {
Expand Down Expand Up @@ -251,3 +258,29 @@ export const toSSG: ToSSGInterface = async (app, fs, options) => {
await options?.afterGenerateHook?.(result)
return result
}

/**
* @experimental
* `disableSSG` is an experimental feature.
* The API might be changed.
*/

export const disableSSG = (): MiddlewareHandler =>
async function disableSSG(c, next) {
await next()
c.header(X_HONO_DISABLE_SSG_HEADER_KEY, 'true')
}

/**
* @experimental
* `onlySSG` is an experimental feature.
* The API might be changed.
*/
export const onlySSG = (): MiddlewareHandler =>
async function onlySSG(c, next) {
const headerValue = c.req.raw.headers.get(X_HONO_SSG_HEADER_KEY)
if (headerValue) {
await next()
}
return c.notFound()
}