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

refactor(ssg): create request from saved requestInit in order to avoid memory leak warnings #2186

Merged
merged 2 commits into from
Feb 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -148,9 +148,13 @@ export const fetchRoutesContent = async <
forGetInfoURLRequest.ssgParams = [{}]
}

const requestInit = {
method: forGetInfoURLRequest.method,
headers: forGetInfoURLRequest.headers,
}
for (const param of forGetInfoURLRequest.ssgParams) {
const replacedUrlParam = replaceUrlParam(route.path, param)
let response = await app.request(replacedUrlParam, forGetInfoURLRequest)
let response = await app.request(replacedUrlParam, requestInit)
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) {
continue
}
Expand Down
21 changes: 21 additions & 0 deletions src/helper/ssg/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,27 @@ describe('toSSG function', () => {
expect(afterGenerateHookMock).toHaveBeenCalled()
expect(afterGenerateHookMock).toHaveBeenCalledWith(expect.anything())
})

it('should avoid memory leak from `req.signal.addEventListener()`', async () => {
const fsMock: FileSystemModule = {
writeFile: vi.fn(() => Promise.resolve()),
mkdir: vi.fn(() => Promise.resolve()),
}

const signalAddEventListener = vi.fn(() => {})
const app = new Hono()
app.get('/post/:post', ssgParams([{ post: '1' }, { post: '2' }]), (c) =>
c.html(<h1>{c.req.param('post')}</h1>)
)
await toSSG(app, fsMock, {
beforeRequestHook: (req) => {
req.signal.addEventListener = signalAddEventListener
return req
},
})

expect(signalAddEventListener).not.toHaveBeenCalled()
})
})

describe('fetchRoutesContent function', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ export const fetchRoutesContent = async <
forGetInfoURLRequest.ssgParams = [{}]
}

const requestInit = {
method: forGetInfoURLRequest.method,
headers: forGetInfoURLRequest.headers,
}
for (const param of forGetInfoURLRequest.ssgParams) {
const replacedUrlParam = replaceUrlParam(route.path, param)
let response = await app.request(replacedUrlParam, forGetInfoURLRequest)
let response = await app.request(replacedUrlParam, requestInit)
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) {
continue
}
Expand Down