Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4c66757
multithreading WIP
vezaynk Apr 27, 2023
a49fdff
More WIP
vezaynk Apr 27, 2023
b956569
more WIP
vezaynk Apr 27, 2023
e1e88ab
apply fix
vezaynk Apr 28, 2023
fce6f6e
Dev and Prod working
vezaynk Apr 28, 2023
a8fb7a8
Fix up tests
vezaynk Apr 28, 2023
aef4297
Remove Worker.js
vezaynk Apr 29, 2023
8892002
Streaming works
vezaynk May 4, 2023
34e6535
Setup worker pool
vezaynk May 5, 2023
d305a74
Setup preloading
vezaynk May 5, 2023
c9b919b
Add transform support
vezaynk May 5, 2023
19002c7
Cleanup 1
vezaynk May 5, 2023
20df079
One more ignore
vezaynk May 5, 2023
9598aa8
bump dev dependencies
vezaynk May 8, 2023
d1f4dd1
Use `satisfies` on postMessage
vezaynk May 8, 2023
108bafc
Building works
vezaynk May 16, 2023
4a6f97b
Forward more types into pass-through
vezaynk May 16, 2023
f9b6aa5
Try-catch load ts-node
vezaynk May 16, 2023
8e8065e
Add comment
vezaynk May 16, 2023
0c95835
Set up transform hook test
vezaynk May 16, 2023
a82434f
Merge branch 'main' of https://github.com/gadget-inc/fastify-renderer
vezaynk Nov 2, 2023
2180e04
Things are cooking, but not working
vezaynk Nov 3, 2023
879f579
It's alive!
vezaynk Nov 3, 2023
a5fab24
lint
vezaynk Nov 3, 2023
94a23bc
Merge branch 'work-on-working'
vezaynk Nov 3, 2023
7d0e154
some cleanup
vezaynk Nov 3, 2023
4f67ad1
Some test cleanup
vezaynk Nov 3, 2023
ff8043e
Improving semantics
vezaynk Nov 3, 2023
0a99ad3
nonce WIP
vezaynk Nov 4, 2023
eee3c2b
Nonces work
vezaynk Nov 4, 2023
4951bba
Trying to fix tests
vezaynk Nov 4, 2023
fd33bae
Disable a bunch of tests
vezaynk Nov 4, 2023
0118f1b
version bumpings
vezaynk Nov 4, 2023
978cf5a
Fixing ts issues
vezaynk Nov 4, 2023
271765d
mo' version bumps, mo' problems
vezaynk Nov 4, 2023
0b26823
Merge branch 'main' of github.com:gadget-inc/fastify-renderer
vezaynk Nov 4, 2023
94bf9dc
More cleanup
vezaynk Nov 4, 2023
535c89b
remove settings
vezaynk Nov 4, 2023
866dd60
partially disable tests
vezaynk Nov 4, 2023
50af9e1
lint
vezaynk Nov 4, 2023
44b191c
pass root tests
vezaynk Nov 18, 2023
7a6a0ba
handle error
vezaynk Nov 18, 2023
7d9bc42
add some error handling
vezaynk Nov 18, 2023
e38fcc7
Upgrade to React 18
vezaynk Nov 18, 2023
e705dc0
fix deprecation warning
vezaynk Nov 18, 2023
aec6e19
add error stream
vezaynk Nov 18, 2023
e57a48e
fix useTransition
vezaynk Nov 18, 2023
858b98f
Demo React 18 Upgrade Errors
vezaynk Nov 19, 2023
3dd81d5
Jest to Vitest migration
vezaynk Nov 19, 2023
8de744d
Merge branch 'upgrade-react'
vezaynk Nov 19, 2023
1eb6484
All tests go
vezaynk Nov 19, 2023
8970f50
unskip test
vezaynk Nov 19, 2023
d61fc28
Add a couple tests
vezaynk Nov 19, 2023
c7a9585
improve consistency between dev and prod
vezaynk Nov 19, 2023
d89ac3b
Add defineRenderHook
vezaynk Nov 19, 2023
a20e882
remove error hook from server
vezaynk Nov 19, 2023
9eca2bc
Write test error hook
vezaynk Nov 19, 2023
6367fc7
fix comment
vezaynk Nov 19, 2023
7b160ee
remove excessive itmeout
vezaynk Nov 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export class ReactRenderer implements Renderer {
this.renderables = renderables
this.devServer = devServer

this.transformHooks = this.plugin.hooks
.map(unthunk)
.map((hook) => hook.transform?.absolutePath)
.flatMap((hook) => (hook ? [hook] : []))

// in production mode, we eagerly require all the endpoints during server boot, so that the first request to the endpoint isn't slow
// if the service running fastify-renderer is being gracefully restarted, this will block the fastify server from listening until all the code is required, keeping the old server in service a bit longer while this require is done, which is good for users
if (!this.plugin.devMode) {
Expand All @@ -59,10 +64,6 @@ export class ReactRenderer implements Renderer {
}

const mode = this.options.mode
this.transformHooks = this.plugin.hooks
.map(unthunk)
.map((hook) => hook.transform?.absolutePath)
.flatMap((hook) => (hook ? [hook] : []))

const modulePaths = await this.getPreloadPaths()
const paths = [...modulePaths, ...this.transformHooks]
Expand Down
4 changes: 2 additions & 2 deletions packages/fastify-renderer/src/node/renderers/react/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export function staticRender({ bootProps, destination, renderBase, module, hooks
)
)

const transformers = hooks.map((hook) => require(hook))
const transformers = hooks.map((hook) => require(hook)!.default)

for (const hook of transformers) {
app = hook.transform(app)
app = hook(app)
}

return (ReactDOMServer as typeof _ReactDOMServer).renderToString(app)
Expand Down
6 changes: 6 additions & 0 deletions packages/fastify-renderer/test/fixtures/transformer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react'
export default function Wrapped(app) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return React.createElement(React.Fragment, null, React.createElement('h1', null, 'Transform Hook'), app)
}
40 changes: 25 additions & 15 deletions packages/fastify-renderer/test/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import { FastifyRendererOptions } from '../node/Plugin'
import FastifyRenderer from '../src/node'
import { unthunk } from '../src/node/utils'
import { newFastify } from './helpers'
Expand All @@ -7,7 +8,7 @@ const testComponent = require.resolve(path.join(__dirname, 'fixtures', 'test-mod
const testLayoutComponent = require.resolve(path.join(__dirname, 'fixtures', 'test-layout.tsx'))
let thunkId = 0

const options = {
const options: FastifyRendererOptions = {
vite: { root: __dirname, logLevel: (process.env.LOG_LEVEL ?? 'info') as any },
devMode: true,
renderer: {
Expand All @@ -19,9 +20,6 @@ const options = {
heads: () => {
return 'head'
},
transform: (app) => {
return app
},
postRenderHeads: () => {
return 'postRenderHead'
},
Expand All @@ -33,19 +31,22 @@ const options = {
heads: () => {
return `<style>#${id} {}</style>`
},
transform: (app) => {
return app
},
postRenderHeads: () => {
return ''
},
}
},
{
name: 'Test transform hook',
transform: {
absolutePath: require.resolve('./fixtures/transformer.jsx'),
},
},
],
}

describe('FastifyRenderer', () => {
let server
let server: Awaited<ReturnType<typeof newFastify>>
beforeAll(async () => {
server = await newFastify()
await server.register(FastifyRenderer, options)
Expand All @@ -59,6 +60,9 @@ describe('FastifyRenderer', () => {
server.get(
'/early-hook-reply',
{
// TODO: Investigate linter error
// https://typescript-eslint.io/rules/no-misused-promises/#checksvoidreturn
// eslint-disable-next-line @typescript-eslint/no-misused-promises
preValidation: async (_request, reply) => {
await reply.code(201).send('hello')
},
Expand Down Expand Up @@ -116,18 +120,14 @@ describe('FastifyRenderer', () => {
expect(response.body).toEqual('hello')
})

// TODO: Re-introduce transformation hooks
test.skip('should call hooks in correct order', async () => {
test('should call hooks in correct order', async () => {
const callOrder: string[] = []
if (!options.hooks) throw new Error('Test options are not setup correctly')
const hook = unthunk(options.hooks[0])
jest.spyOn(hook, 'heads').mockImplementation(() => {
callOrder.push('heads')
return 'head'
})
jest.spyOn(hook, 'transform').mockImplementation((app) => {
callOrder.push('transforms')
return app
})
jest.spyOn(hook, 'postRenderHeads').mockImplementation(() => {
callOrder.push('postRenders')
return 'postRender'
Expand All @@ -139,7 +139,7 @@ describe('FastifyRenderer', () => {
headers: { Accept: 'text/html' },
})

expect(callOrder).toEqual(['transforms', 'heads', 'postRenders'])
expect(callOrder).toEqual(['heads', 'postRenders'])
})

test('should unthunk hooks on every render', async () => {
Expand All @@ -158,4 +158,14 @@ describe('FastifyRenderer', () => {
expect(firstResponse.body).toMatch('<style>#0 {}</style>')
expect(secondResponse.body).toMatch('<style>#1 {}</style>')
})

test('should run transform hooks', async () => {
const response = await server.inject({
method: 'GET',
url: '/render-test',
headers: { Accept: 'text/html' },
})

expect(response.body).toContain('Transform Hook')
})
})