Skip to content

Commit

Permalink
Merge pull request #208 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(response): remove render function
  • Loading branch information
jlenon7 authored Jan 16, 2025
2 parents c4912d6 + 0b9fe31 commit ba595e0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 66 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.22.0",
"version": "5.23.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -82,7 +82,7 @@
"@athenna/test": "^5.2.0",
"@athenna/tsconfig": "^5.0.0",
"@athenna/view": "^5.1.0",
"@athenna/vite": "^5.9.0",
"@athenna/vite": "^5.10.0",
"@fastify/cors": "^10.0.1",
"@fastify/helmet": "^13.0.0",
"@fastify/rate-limit": "^10.2.1",
Expand Down
58 changes: 0 additions & 58 deletions src/context/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@

import { View } from '@athenna/view'
import type { FastifyReply } from 'fastify'
import { Server } from '#src/facades/Server'
import { Module, Options } from '@athenna/common'
import type { SendOptions } from '@fastify/static'
import type { Request } from '#src/context/Request'
import type { FastifyHelmetOptions } from '@fastify/helmet'

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

export class Response {
/**
* The fastify response object.
Expand Down Expand Up @@ -146,60 +142,6 @@ export class Response {
return this.html(content)
}

/**
* Terminate the request sending a React component to be rendered.
*
* @example
* ```ts
* return response.render('index')
* return response.render('index', {
* component: 'src/resources/app/app.tsx',
* viewData: {},
* beforeComponentRender: (component) => {
* return component.createApp()
* }
* })
* ```
*/
public async render(
view: string,
options?: {
component?: string
viewData?: any
beforeComponentRender?: (componentModule: any) => any
}
) {
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'),
beforeComponentRender: options?.component
? null
: component => {
return component.createApp(this.request.baseUrl)
}
})

const vite = Server.getVitePlugin().getVite()
let component = await vite.ssrLoadModule(options.component)

if (options.beforeComponentRender) {
component = options.beforeComponentRender(component)
}

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

/**
* Terminate the request sending the response body or not.
*
Expand Down

0 comments on commit ba595e0

Please sign in to comment.