Skip to content

Commit

Permalink
Merge pull request #206 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(response): use safeImport
  • Loading branch information
jlenon7 authored Jan 15, 2025
2 parents ba2ac31 + 23d38a0 commit 69ff689
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.20.0",
"version": "5.21.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
12 changes: 8 additions & 4 deletions src/context/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { SendOptions } from '@fastify/static'
import type { Request } from '#src/context/Request'
import type { FastifyHelmetOptions } from '@fastify/helmet'

const react = Module.safeImport('react') as any
let reactDom = Module.safeImport('react-dom/server') as any

export class Response {
/**
Expand Down Expand Up @@ -169,10 +169,14 @@ export class Response {
beforeComponentRender?: (componentModule: any) => any
}
) {
if (!react) {
throw new Error('React is not installed, please run "npm i react".')
if (!reactDom) {
throw new Error(
'React is not installed, please run "npm i react react-dom".'
)
}

reactDom = await reactDom

options = Options.create(options, {
viewData: {},
component: Config.get('http.vite.ssrEntrypoint'),
Expand All @@ -191,7 +195,7 @@ export class Response {
}

return this.view(view, {
element: react.renderToString(component),
element: reactDom.renderToString(component),
...options.viewData
})
}
Expand Down

0 comments on commit 69ff689

Please sign in to comment.