Skip to content
Myr

refactor(ssg): filter SSG Route (#2181)

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

* filter * refactor * move dev to util * simply loop

authored by watany · committed by GitHub · parent a49d4e4 · Browse files · 9 files+73 added−34 removed
This commit

State

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

Diff stats

9 files+73 added−34 removed

  • tests:+5 added−2 removed
  • other:+68 added−32 removed

9 files+73 added−34 removed

modified:deno_dist/helper/dev/index.ts+1 added-9 removed
modified:deno_dist/helper/ssg/index.ts+2 added-7 removed
1import { replaceUrlParam } from '../../client/utils.ts'2import type { Context } from '../../context.ts'3import { inspectRoutes } from '../../helper/dev/index.ts'3import type { Hono } from '../../hono.ts'4import type { Env, MiddlewareHandler, Schema } from '../../types.ts'5import { bufferToString } from '../../utils/buffer.ts'6import { getExtension } from '../../utils/mime.ts'8import { joinPaths, dirname } from './utils.ts'7import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils.ts'89export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'10export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'11
108 unmodified lines
120): Promise<Map<string, { content: string | ArrayBuffer; mimeType: string }>> => {121 const htmlMap = new Map<string, { content: string | ArrayBuffer; mimeType: string }>()122 const baseURL = 'http://localhost'123125 for (const route of inspectRoutes(app)) {126 if (route.isMiddleware) {127 continue128 }129124 for (const route of filterStaticGenerateRoutes(app)) {125 // GET Route Info126 const thisRouteBaseURL = new URL(route.path, baseURL).toString()127128 let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest
157 unmodified lines
modified:deno_dist/helper/ssg/utils.ts+21 added
added:deno_dist/utils/handler.ts+10 added
modified:src/helper/dev/index.ts+1 added-9 removed
1import type { Hono } from '../../hono'2import { COMPOSED_HANDLER } from '../../hono-base'2import type { Env, RouterRoute } from '../../types'3import { findTargetHandler, isMiddleware } from '../../utils/handler'45interface ShowRoutesOptions {6 verbose?: boolean7 colorize?: boolean
5 unmodified lines
13 name: string14 isMiddleware: boolean15}1617const isMiddleware = (handler: Function) => handler.length > 117const handlerName = (handler: Function) => {18 return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')19}21const findTargetHandler = (handler: Function): Function => {22 // eslint-disable-next-line @typescript-eslint/no-explicit-any23 return (handler as any)[COMPOSED_HANDLER]24 ? // eslint-disable-next-line @typescript-eslint/no-explicit-any25 findTargetHandler((handler as any)[COMPOSED_HANDLER])26 : handler27}2021export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {22 return hono.routes.map(({ path, method, handler }: RouterRoute) => {23 const targetHandler = findTargetHandler(handler)
59 unmodified lines
modified:src/helper/ssg/index.test.tsx+5 added-2 removed
149 unmodified lines
150151 expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', expect.any(String))152 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))153 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about/some.txt', expect.any(String))154 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about/some/thing.txt', expect.any(String))154 expect(fsMock.writeFile).not.toHaveBeenCalledWith(155 'static/about/some/thing.txt',156 expect.any(String)157 )158 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))159 expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.html', expect.any(String))160 expect(fsMock.writeFile).toHaveBeenCalledWith('static/Charlie.html', expect.any(String))161 })
9 unmodified lines
171 }172 return false173 }174 const result = await toSSG(app, fsMock, { beforeRequestHook })172 expect(result.files).toHaveLength(11)175 expect(result.files).toHaveLength(10)176 })177178 it('should skip the route if the request hook returns false', async () => {179 const beforeRequest: BeforeRequestHook = () => false
194 unmodified lines
modified:src/helper/ssg/index.ts+2 added-7 removed
1import { replaceUrlParam } from '../../client/utils'2import type { Context } from '../../context'3import { inspectRoutes } from '../../helper/dev'3import type { Hono } from '../../hono'4import type { Env, MiddlewareHandler, Schema } from '../../types'5import { bufferToString } from '../../utils/buffer'6import { getExtension } from '../../utils/mime'8import { joinPaths, dirname } from './utils'7import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils'89export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'10export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'11
108 unmodified lines
120): Promise<Map<string, { content: string | ArrayBuffer; mimeType: string }>> => {121 const htmlMap = new Map<string, { content: string | ArrayBuffer; mimeType: string }>()122 const baseURL = 'http://localhost'123125 for (const route of inspectRoutes(app)) {126 if (route.isMiddleware) {127 continue128 }129124 for (const route of filterStaticGenerateRoutes(app)) {125 // GET Route Info126 const thisRouteBaseURL = new URL(route.path, baseURL).toString()127128 let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest
157 unmodified lines
modified:src/helper/ssg/utils.ts+21 added
1import type { Hono } from '../../hono'2import { METHOD_NAME_ALL } from '../../router'3import type { Env, RouterRoute } from '../../types'4import { findTargetHandler, isMiddleware } from '../../utils/handler'56/**7 * Get dirname8 * @param path File Path9 * @returns Parent dir path
39 unmodified lines
49 const resultPaths: string[] = []50 handleSegments(paths.join('/').split('/'), resultPaths)51 return (paths[0][0] === '/' ? '/' : '') + resultPaths.join('/')52}5354interface FilterStaticGenerateRouteData {55 path: string56}5758export const filterStaticGenerateRoutes = <E extends Env>(59 hono: Hono<E>60): FilterStaticGenerateRouteData[] => {61 return hono.routes.reduce((acc, { method, handler, path }: RouterRoute) => {62 const targetHandler = findTargetHandler(handler)63 if (['GET', METHOD_NAME_ALL].includes(method) && !isMiddleware(targetHandler)) {64 acc.push({ path })65 }66 return acc67 }, [] as FilterStaticGenerateRouteData[])68}
added:src/utils/handler.ts+10 added
1import { COMPOSED_HANDLER } from '../hono-base'23export const isMiddleware = (handler: Function) => handler.length > 14export const findTargetHandler = (handler: Function): Function => {5 // eslint-disable-next-line @typescript-eslint/no-explicit-any6 return (handler as any)[COMPOSED_HANDLER]7 ? // eslint-disable-next-line @typescript-eslint/no-explicit-any8 findTargetHandler((handler as any)[COMPOSED_HANDLER])9 : handler10}