Skip to content

Commit

Permalink
refactor: cache vite runtime when using SSR
Browse files Browse the repository at this point in the history
Close #31
  • Loading branch information
Julien-R44 committed Sep 30, 2024
1 parent aaf1c0a commit 383c4c2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/server_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Vite } from '@adonisjs/vite'

import { pathToFileURL } from 'node:url'
import type { PageObject, RenderInertiaSsrApp, ResolvedConfig } from './types.js'
import type { ViteRuntime } from 'vite/runtime'

/**
* Responsible for rendering page on the server
Expand All @@ -20,6 +21,8 @@ import type { PageObject, RenderInertiaSsrApp, ResolvedConfig } from './types.js
* bundle generated by Vite
*/
export class ServerRenderer {
static runtime: ViteRuntime

constructor(
protected config: ResolvedConfig,
protected vite?: Vite
Expand All @@ -40,8 +43,8 @@ export class ServerRenderer {
* if we are in development mode
*/
if (devServer) {
const runtime = await this.vite!.createRuntime()
render = await runtime.executeEntrypoint(this.config.ssr.entrypoint!)
ServerRenderer.runtime ??= await this.vite!.createRuntime()
render = await ServerRenderer.runtime.executeEntrypoint(this.config.ssr.entrypoint!)
} else {
/**
* Otherwise, just import the SSR bundle
Expand Down

0 comments on commit 383c4c2

Please sign in to comment.