Skip to content

Commit

Permalink
dev ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 11, 2024
1 parent 3701734 commit 4fdff4b
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 198 deletions.
1 change: 1 addition & 0 deletions packages/core/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const builds = [
{ entryPoints: ['src/index.ts'], format: 'cjs', outfile: 'dist/index.js', platform: 'browser' },
{ entryPoints: ['src/server.ts'], format: 'esm', outfile: 'dist/server.esm.js', platform: 'node' },
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
{ entryPoints: ['src/vite.ts'], format: 'esm', outfile: 'dist/vite.js', platform: 'node' },
]

builds.forEach((build) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"types": "./types/server.d.ts",
"import": "./dist/server.esm.js",
"require": "./dist/server.js"
},
"./vite": {
"types": "./types/vite.d.ts",
"default": "./dist/vite.js"
}
},
"typesVersions": {
Expand Down
14 changes: 4 additions & 10 deletions packages/core/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { createServer, IncomingMessage } from 'http'
import * as process from 'process'
import { InertiaAppResponse, Page } from './types'
import { readableToString } from './serverUtils'
import type { InertiaAppResponse, Page } from './types'

type AppCallback = (page: Page) => InertiaAppResponse
type RouteHandler = (request: IncomingMessage) => Promise<unknown>
export type AppCallback = (page: Page) => InertiaAppResponse

const readableToString: (readable: IncomingMessage) => Promise<string> = (readable) =>
new Promise((resolve, reject) => {
let data = ''
readable.on('data', (chunk) => (data += chunk))
readable.on('end', () => resolve(data))
readable.on('error', (err) => reject(err))
})
type RouteHandler = (request: IncomingMessage) => Promise<unknown>

export default (render: AppCallback, port?: number): void => {
const _port = port || 13714
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/serverUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { IncomingMessage } from 'http'

export const readableToString: (readable: IncomingMessage) => Promise<string> = (readable) =>
new Promise((resolve, reject) => {
let data = ''
readable.on('data', (chunk) => (data += chunk))
readable.on('end', () => resolve(data))
readable.on('error', (err) => reject(err))
})
28 changes: 28 additions & 0 deletions packages/core/src/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Plugin } from 'vite'
import { readableToString } from './serverUtils'

interface PluginConfig {
renderer: string
}

export default function inertia(config: PluginConfig): Plugin {
// todo: validate config

return {
name: '@inertiajs/svelte/ssr',
async configureServer(server) {
return () =>
server.middlewares.use(async (req, res, next) => {
if (req.url !== '/render') {
next()
}

// todo: check if render is a function
const { render } = await server.ssrLoadModule(config.renderer)
const response = await render(JSON.parse(await readableToString(req)))
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(response))
})
},
}
}
1 change: 1 addition & 0 deletions packages/react/src/server.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@inertiajs/core/server'
export { default as default } from '@inertiajs/core/server'
1 change: 1 addition & 0 deletions packages/svelte/src/server.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@inertiajs/core/server'
export { default as default } from '@inertiajs/core/server'
13 changes: 11 additions & 2 deletions playgrounds/react/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"require": {
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"inertiajs/inertia-laravel": "2.x-dev",
"inertiajs/inertia-laravel": "@dev",
"laravel/framework": "^10.0",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.7"
Expand Down Expand Up @@ -65,5 +65,14 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"repositories": [
{
"type": "path",
"url": "./../../../inertia-laravel/",
"options": {
"symlink": true
}
}
]
}
39 changes: 15 additions & 24 deletions playgrounds/react/composer.lock

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

12 changes: 12 additions & 0 deletions playgrounds/react/resources/js/viteSsr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createInertiaApp } from '@inertiajs/react'
import * as ReactDOMServer from 'react-dom/server'
import type { AppCallback } from '@inertiajs/svelte/server'

export const render: AppCallback = (page) =>
createInertiaApp({
page,
render: ReactDOMServer.renderToString,
title: (title) => `${title} - React Playground`,
resolve: (name) => import(`./Pages/${name}.tsx`),
setup: ({ App, props }) => <App {...props} />,
})
4 changes: 4 additions & 0 deletions playgrounds/react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inertia from '@inertiajs/core/vite'
import react from '@vitejs/plugin-react'
import laravel from 'laravel-vite-plugin'
import { defineConfig } from 'vite'
Expand All @@ -9,6 +10,9 @@ export default defineConfig({
ssr: 'resources/js/ssr.tsx',
refresh: true,
}),
inertia({
renderer: 'resources/js/viteSsr.tsx',
}),
react({}),
],
})
6 changes: 3 additions & 3 deletions playgrounds/svelte4/composer.lock

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

Loading

0 comments on commit 4fdff4b

Please sign in to comment.