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

chore(response): remove render function #208

Merged
merged 1 commit into from
Jan 16, 2025
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
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 <[email protected]>",
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
Loading