feat(ssg): add default plugin that defines the recommended behavior (#4394)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(ssg): add default plugin that defines the recommended behavior * test(ssg): add tests to handle redirects by default plugin * fix(ssg): set default plugin in options if none provided * test(ssg): add test for default plugin usage when plugins option is omitted * fix(ssg): update default plugin to skip non-200 responses
authored by 3w36zj6 · committed by GitHub · parent 0133317 · Browse files · 2 files·+54 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−4 removed
- tests:+37 added−3 removed
- other:+17 added−1 removed
Provenance
- Commit
fb2a7ef- Tree
d979dcc- Author
3w36zj6 <52315048+3w36zj6@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0133317
2 files changed+54 added−4 removed
modified:src/helper/ssg/ssg.test.tsx+37 added-3 removed
7 unmodified lines8 isSSGContext,9 onlySSG,10 ssgParams,11 } from './middleware'12-import { defaultExtensionMap, fetchRoutesContent, saveContentToFile, toSSG } from './ssg'12+import {13+ defaultExtensionMap,14+ fetchRoutesContent,15+ saveContentToFile,16+ toSSG,17+ defaultPlugin,18+} from './ssg'19 import type {20 AfterGenerateHook,21 AfterResponseHook,22 BeforeRequestHook,808 unmodified lines831 app = new Hono()832 app.get('/', (c) => c.html('<h1>Home</h1>'))833 app.get('/about', (c) => c.html('<h1>About</h1>'))834 app.get('/blog', (c) => c.html('<h1>Blog</h1>'))835+ app.get('/created', (c) => c.text('201 Created', 201))836+ app.get('/redirect', (c) => c.redirect('/'))837+ app.get('/notfound', (c) => c.notFound())838+ app.get('/error', (c) => c.text('500 Error', 500))839 840 fsMock = {841 writeFile: vi.fn(() => Promise.resolve()),842 mkdir: vi.fn(() => Promise.resolve()),843 }844 })845 846+ it('should use defaultPlugin when plugins option is omitted', async () => {847+ // @ts-expect-error defaultPlugin has afterResponseHook848+ const defaultPluginSpy = vi.spyOn(defaultPlugin, 'afterResponseHook')849+ await toSSG(app, fsMock, { dir: './static' })850+ expect(defaultPluginSpy).toHaveBeenCalled()851+ defaultPluginSpy.mockRestore()852+ })853+854+ it('should skip non-200 responses with defaultPlugin', async () => {855+ const result = await toSSG(app, fsMock, { plugins: [defaultPlugin], dir: './static' })856+ expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', '<h1>Home</h1>')857+ expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', '<h1>About</h1>')858+ expect(fsMock.writeFile).toHaveBeenCalledWith('static/blog.html', '<h1>Blog</h1>')859+ expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/created.txt', expect.any(String))860+ expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/redirect.txt', expect.any(String))861+ expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/notfound.txt', expect.any(String))862+ expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/error.txt', expect.any(String))863+ expect(result.files.some((f) => f.includes('created'))).toBe(false)864+ expect(result.files.some((f) => f.includes('redirect'))).toBe(false)865+ expect(result.files.some((f) => f.includes('notfound'))).toBe(false)866+ expect(result.files.some((f) => f.includes('error'))).toBe(false)867+ expect(result.success).toBe(true)868+ })869+870 it('should correctly apply plugins with beforeRequestHook', async () => {871 const plugin: SSGPlugin = {872 beforeRequestHook: (req) => {873 // Skip requests to the blog page8 unmodified lines882 const result = await toSSG(app, fsMock, {883 plugins: [plugin],884 })885 852- expect(result.files).toHaveLength(2) // Home and About pages only886+ expect(result.files).toHaveLength(6)887 expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', '<h1>Home</h1>')888 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', '<h1>About</h1>')889 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/blog.html', '<h1>Blog</h1>')890 })58 unmodified lines949 const result = await toSSG(app, fsMock, {950 plugins: [skipBlogPlugin, prefixPlugin, sitemapPlugin],951 })952 919- expect(result.files).toHaveLength(3) // Home, About, and sitemap.xml953+ expect(result.files).toHaveLength(7)954 expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', '[Prefix] <h1>Home</h1>')955 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', '[Prefix] <h1>About</h1>')956 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/blog.html', expect.any(String))957 expect(result.files).toContain('sitemap.xml')73 unmodified linesmodified:src/helper/ssg/ssg.ts+17 added-1 removed
347 unmodified lines348 (app: Hono<E, S, BasePath>, options?: ToSSGOptions): Promise<ToSSGResult>349 }350 351 /**352+ * The default plugin that defines the recommended behavior.353+ *354 * @experimental355+ * `defaultPlugin` is an experimental feature.356+ * The API might be changed.357+ */358+export const defaultPlugin: SSGPlugin = {359+ afterResponseHook: (res) => {360+ if (res.status !== 200) {361+ return false362+ }363+ return res364+ },365+}366+367+/**368+ * @experimental369 * `toSSG` is an experimental feature.370 * The API might be changed.371 */372 export const toSSG: ToSSGInterface = async (app, fs, options) => {373 let result: ToSSGResult | undefined374 const getInfoPromises: Promise<unknown>[] = []375 const savePromises: Promise<string | undefined>[] = []360- const plugins = options?.plugins || []376+ const plugins = options?.plugins || [defaultPlugin]377 const beforeRequestHooks: BeforeRequestHook[] = []378 const afterResponseHooks: AfterResponseHook[] = []379 const afterGenerateHooks: AfterGenerateHook[] = []380 if (options?.beforeRequestHook) {94 unmodified lines