Skip to content
Myr

fix(ssg): invoke callback when it's only a dynamic route (#4249)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(ssg): invoke callback when it's only a dynamic route Co-authored-by: Taku Amano <taku@taaas.jp> * should not invoke handler after ssgParams for dynamic route request ---------

authored by Yusuke Wada · committed by GitHub · parent 0432f81 · Browse files · 4 files+49 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

4 files+49 added−6 removed

  • tests:+39 added−0 removed
  • other:+10 added−6 removed

4 files+49 added−6 removed

modified:src/helper/ssg/middleware.ts+5 added-1 removed
modified:src/helper/ssg/ssg.test.tsx+39 added
953 unmodified lines
954 '<h1>Blog</h1> [Option] [Plugin]'955 )956 })957})958959describe('ssgParams', () => {960 it('should invoke callback only once', async () => {961 const app = new Hono()962 const cb = vi.fn(() => [{ post: '1' }, { post: '2' }])963 app.get('/post/:post', ssgParams(cb), (c) => c.html(<h1>{c.req.param('post')}</h1>))964 const fsMock: FileSystemModule = {965 writeFile: vi.fn(() => Promise.resolve()),966 mkdir: vi.fn(() => Promise.resolve()),967 }968 await toSSG(app, fsMock)969970 expect(cb).toHaveBeenCalledTimes(1)971 })972973 it('should not invoke handler after ssgParams for dynamic route request', async () => {974 const app = new Hono()975 const log = vi.fn()976 app.get(977 '/shops/:id',978 ssgParams(() => [{ id: 'shop1' }]),979 async (c) => {980 const id = c.req.param('id')981 log(id)982 return c.html(id)983 }984 )985 const fsMock: FileSystemModule = {986 writeFile: vi.fn(() => Promise.resolve()),987 mkdir: vi.fn(() => Promise.resolve()),988 }989 await toSSG(app, fsMock)990991 expect(log).toHaveBeenCalledTimes(1)992 expect(log).toHaveBeenCalledWith('shop1')993 expect(fsMock.writeFile).toHaveBeenCalledTimes(1)994 expect(fsMock.writeFile).toHaveBeenCalledWith('static/shops/shop1.html', 'shop1')995 })996})
modified:src/helper/ssg/ssg.ts+1 added-5 removed
3 unmodified lines
4import { createPool } from '../../utils/concurrent'5import { getExtension } from '../../utils/mime'6import type { AddedSSGDataRequest, SSGParams } from './middleware'7import { SSG_CONTEXT, X_HONO_DISABLE_SSG_HEADER_KEY } from './middleware'8import { dirname, filterStaticGenerateRoutes, joinPaths } from './utils'8import { dirname, filterStaticGenerateRoutes, isDynamicRoute, joinPaths } from './utils'910const DEFAULT_CONCURRENCY = 2 // default concurrency for ssg1112// 'default_content_type' is designed according to Bun's performance optimization,
266 unmodified lines
279 })280 }281}282283const isDynamicRoute = (path: string): boolean => {284 return path.split('/').some((segment) => segment.startsWith(':') || segment.includes('*'))285}286283/**284 * @experimental285 * `saveContentToFile` is an experimental feature.286 * The API might be changed.
163 unmodified lines
modified:src/helper/ssg/utils.ts+4 added