Skip to content

Commit

Permalink
fix(ssg): Preserve the SSR server instance during page rendering (#209)
Browse files Browse the repository at this point in the history
* test(ssg): handle a page with dynamic imports

* fix(ssg): keep server instance during render the pages

* yarn changeset
  • Loading branch information
berlysia authored Jan 9, 2025
1 parent 2179833 commit 9a39a51
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curly-cherries-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-ssg': patch
---

Preserve the SSR server instance during page rendering
3 changes: 2 additions & 1 deletion packages/ssg/src/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export const ssgBuild = (options?: SSGOptions): Plugin => {
build: { ssr: true },
})
const module = await server.ssrLoadModule(entry)
server.close()

const app = module['default'] as Hono

Expand Down Expand Up @@ -84,6 +83,8 @@ export const ssgBuild = (options?: SSGOptions): Plugin => {
{ dir: outDir }
)

server.close()

if (!result.success) {
throw result.error
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ssg/test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ app.get('/', (c) => {
return c.html('<html><body><h1>Hello!</h1></body></html>')
})

app.get('/dynamic-import', async (c) => {
// @ts-expect-error
const module = await import('./sample.js')
return c.text('Dynamic import works: ' + module.default)
})

export default app
1 change: 1 addition & 0 deletions packages/ssg/test/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "sample!";
6 changes: 6 additions & 0 deletions packages/ssg/test/ssg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('ssgPlugin', () => {
const entryFile = './test/app.ts'
const outDir = path.resolve(testDir, 'dist')
const outputFile = path.resolve(outDir, 'index.html')
const outputFileWithDynamicImport = path.resolve(outDir, 'dynamic-import.txt')

beforeAll(() => {
fs.mkdirSync(testDir, { recursive: true })
Expand Down Expand Up @@ -38,6 +39,11 @@ describe('ssgPlugin', () => {
const output = fs.readFileSync(outputFile, 'utf-8')
expect(output).toBe('<html><body><h1>Hello!</h1></body></html>')

expect(fs.existsSync(outputFileWithDynamicImport)).toBe(true)

const outputDynamicImport = fs.readFileSync(outputFileWithDynamicImport, 'utf-8')
expect(outputDynamicImport).toBe('Dynamic import works: sample!')

// Should not output files corresponding to a virtual entry
expect(fs.existsSync(path.resolve(outDir, 'assets'))).toBe(false)
})
Expand Down

0 comments on commit 9a39a51

Please sign in to comment.