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): Ignore Dynamic Route #1990

Merged
merged 7 commits into from
Jan 17, 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
6 changes: 5 additions & 1 deletion deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const fetchRoutesContent = async <
const baseURL = 'http://localhost'

for (const route of inspectRoutes(app)) {
if (route.isMiddleware) continue
if (route.isMiddleware || isDynamicRoute(route.path)) continue

const url = new URL(route.path, baseURL).toString()
const response = await app.fetch(new Request(url))
Expand All @@ -91,6 +91,10 @@ export const fetchRoutesContent = async <
return htmlMap
}

const isDynamicRoute = (path: string): boolean => {
return path.split('/').some((segment) => segment.startsWith(':') || segment.includes('*'))
}

/**
* @experimental
* `saveContentToFiles` is an experimental feature.
Expand Down
31 changes: 31 additions & 0 deletions src/helper/ssg/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,34 @@ describe('saveContentToFiles function', () => {
)
})
})

describe('Dynamic route handling', () => {
let app: Hono
beforeEach(() => {
app = new Hono()
app.get('/shops/:id', (c) => c.html('Shop Page'))
app.get('/shops/:id/:comments([0-9]+)', (c) => c.html('Comments Page'))
app.get('/foo/*', (c) => c.html('Foo Page'))
app.get('/foo:bar', (c) => c.html('Foo Bar Page'))
})

it('should skip /shops/:id dynamic route', async () => {
const htmlMap = await fetchRoutesContent(app)
expect(htmlMap.has('/shops/:id')).toBeFalsy()
})

it('should skip /shops/:id/:comments([0-9]+) dynamic route', async () => {
const htmlMap = await fetchRoutesContent(app)
expect(htmlMap.has('/shops/:id/:comments([0-9]+)')).toBeFalsy()
})

it('should skip /foo/* dynamic route', async () => {
const htmlMap = await fetchRoutesContent(app)
expect(htmlMap.has('/foo/*')).toBeFalsy()
})

it('should not skip /foo:bar dynamic route', async () => {
const htmlMap = await fetchRoutesContent(app)
expect(htmlMap.has('/foo:bar')).toBeTruthy()
})
})
6 changes: 5 additions & 1 deletion src/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const fetchRoutesContent = async <
const baseURL = 'http://localhost'

for (const route of inspectRoutes(app)) {
if (route.isMiddleware) continue
if (route.isMiddleware || isDynamicRoute(route.path)) continue

const url = new URL(route.path, baseURL).toString()
const response = await app.fetch(new Request(url))
Expand All @@ -90,6 +90,10 @@ export const fetchRoutesContent = async <
return htmlMap
}

const isDynamicRoute = (path: string): boolean => {
return path.split('/').some((segment) => segment.startsWith(':') || segment.includes('*'))
}

/**
* @experimental
* `saveContentToFiles` is an experimental feature.
Expand Down