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
Provenance
- Commit
06fcf68- Tree
1ad45b6- Author
watany <76135106+watany-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a49d4e4
9 files changed+73 added−34 removed
modified:deno_dist/helper/dev/index.ts+1 added-9 removed
122345675 unmodified lines1314151617171819212223242526272021222359 unmodified linesimport type { Hono } from '../../hono.ts'import { COMPOSED_HANDLER } from '../../hono-base.ts'import type { Env, RouterRoute } from '../../types.ts'import { findTargetHandler, isMiddleware } from '../../utils/handler.ts'
interface ShowRoutesOptions { verbose?: boolean colorize?: boolean5 unmodified lines name: string isMiddleware: boolean}
const isMiddleware = (handler: Function) => handler.length > 1const handlerName = (handler: Function) => { return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')}const findTargetHandler = (handler: Function): Function => { // eslint-disable-next-line @typescript-eslint/no-explicit-any return (handler as any)[COMPOSED_HANDLER] ? // eslint-disable-next-line @typescript-eslint/no-explicit-any findTargetHandler((handler as any)[COMPOSED_HANDLER]) : handler}
export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => { return hono.routes.map(({ path, method, handler }: RouterRoute) => { const targetHandler = findTargetHandler(handler)59 unmodified linesmodified:deno_dist/helper/ssg/index.ts+2 added-7 removed
1 import { replaceUrlParam } from '../../client/utils.ts'2 import type { Context } from '../../context.ts'3-import { inspectRoutes } from '../../helper/dev/index.ts'3 import type { Hono } from '../../hono.ts'4 import type { Env, MiddlewareHandler, Schema } from '../../types.ts'5 import { bufferToString } from '../../utils/buffer.ts'6 import { getExtension } from '../../utils/mime.ts'8-import { joinPaths, dirname } from './utils.ts'7+import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils.ts'8 9 export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'10 export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'11 108 unmodified lines120 ): Promise<Map<string, { content: string | ArrayBuffer; mimeType: string }>> => {121 const htmlMap = new Map<string, { content: string | ArrayBuffer; mimeType: string }>()122 const baseURL = 'http://localhost'123 125- for (const route of inspectRoutes(app)) {126- if (route.isMiddleware) {127- continue128- }129-124+ for (const route of filterStaticGenerateRoutes(app)) {125 // GET Route Info126 const thisRouteBaseURL = new URL(route.path, baseURL).toString()127 128 let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest157 unmodified linesmodified:deno_dist/helper/ssg/utils.ts+21 added
12345678939 unmodified lines4950515253545556575859606162636465666768import type { Hono } from '../../hono.ts'import { METHOD_NAME_ALL } from '../../router.ts'import type { Env, RouterRoute } from '../../types.ts'import { findTargetHandler, isMiddleware } from '../../utils/handler.ts'
/** * Get dirname * @param path File Path * @returns Parent dir path39 unmodified lines const resultPaths: string[] = [] handleSegments(paths.join('/').split('/'), resultPaths) return (paths[0][0] === '/' ? '/' : '') + resultPaths.join('/')}
interface FilterStaticGenerateRouteData { path: string}
export const filterStaticGenerateRoutes = <E extends Env>( hono: Hono<E>): FilterStaticGenerateRouteData[] => { return hono.routes.reduce((acc, { method, handler, path }: RouterRoute) => { const targetHandler = findTargetHandler(handler) if (['GET', METHOD_NAME_ALL].includes(method) && !isMiddleware(targetHandler)) { acc.push({ path }) } return acc }, [] as FilterStaticGenerateRouteData[])}added:deno_dist/utils/handler.ts+10 added
12345678910import { COMPOSED_HANDLER } from '../hono-base.ts'
export const isMiddleware = (handler: Function) => handler.length > 1export const findTargetHandler = (handler: Function): Function => { // eslint-disable-next-line @typescript-eslint/no-explicit-any return (handler as any)[COMPOSED_HANDLER] ? // eslint-disable-next-line @typescript-eslint/no-explicit-any findTargetHandler((handler as any)[COMPOSED_HANDLER]) : handler}modified:src/helper/dev/index.ts+1 added-9 removed
1 import type { Hono } from '../../hono'2-import { COMPOSED_HANDLER } from '../../hono-base'2 import type { Env, RouterRoute } from '../../types'3+import { findTargetHandler, isMiddleware } from '../../utils/handler'4 5 interface ShowRoutesOptions {6 verbose?: boolean7 colorize?: boolean5 unmodified lines13 name: string14 isMiddleware: boolean15 }16 17-const isMiddleware = (handler: Function) => handler.length > 117 const handlerName = (handler: Function) => {18 return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')19 }21-const 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-}20 21 export 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 linesmodified:src/helper/ssg/index.test.tsx+5 added-2 removed
149 unmodified lines150 151 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 lines171 }172 return false173 }174 const result = await toSSG(app, fsMock, { beforeRequestHook })172- expect(result.files).toHaveLength(11)175+ expect(result.files).toHaveLength(10)176 })177 178 it('should skip the route if the request hook returns false', async () => {179 const beforeRequest: BeforeRequestHook = () => false194 unmodified linesmodified:src/helper/ssg/index.ts+2 added-7 removed
1 import { replaceUrlParam } from '../../client/utils'2 import type { Context } from '../../context'3-import { inspectRoutes } from '../../helper/dev'3 import type { Hono } from '../../hono'4 import type { Env, MiddlewareHandler, Schema } from '../../types'5 import { bufferToString } from '../../utils/buffer'6 import { getExtension } from '../../utils/mime'8-import { joinPaths, dirname } from './utils'7+import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils'8 9 export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'10 export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'11 108 unmodified lines120 ): Promise<Map<string, { content: string | ArrayBuffer; mimeType: string }>> => {121 const htmlMap = new Map<string, { content: string | ArrayBuffer; mimeType: string }>()122 const baseURL = 'http://localhost'123 125- for (const route of inspectRoutes(app)) {126- if (route.isMiddleware) {127- continue128- }129-124+ for (const route of filterStaticGenerateRoutes(app)) {125 // GET Route Info126 const thisRouteBaseURL = new URL(route.path, baseURL).toString()127 128 let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest157 unmodified linesmodified:src/helper/ssg/utils.ts+21 added
1+import type { Hono } from '../../hono'2+import { METHOD_NAME_ALL } from '../../router'3+import type { Env, RouterRoute } from '../../types'4+import { findTargetHandler, isMiddleware } from '../../utils/handler'5+6 /**7 * Get dirname8 * @param path File Path9 * @returns Parent dir path39 unmodified lines49 const resultPaths: string[] = []50 handleSegments(paths.join('/').split('/'), resultPaths)51 return (paths[0][0] === '/' ? '/' : '') + resultPaths.join('/')52 }53+54+interface FilterStaticGenerateRouteData {55+ path: string56+}57+58+export 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
1+import { COMPOSED_HANDLER } from '../hono-base'2+3+export const isMiddleware = (handler: Function) => handler.length > 14+export 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+}