Skip to content

Commit 925c178

Browse files
committed
feat(ssr): support vue with ssr
BREAKING CHANGE: this will require existing SSR users to make changes for SSR to work
1 parent 9c79402 commit 925c178

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

adonis-typings/inertia.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ declare module '@ioc:EidelLev/Inertia' {
5252
*/
5353
ssr?: {
5454
enabled: boolean;
55-
/**
56-
* Which framework should we use to pre-render our response
57-
* NOTE: only `react` is currently supported
58-
*/
59-
mode: 'react' | 'vue2' | 'vue3' | 'svelte';
60-
/**
61-
* Where should inertia look for page components relative to the `resources` directory
62-
* @default js/Pages
63-
*/
64-
pageRootDir?: string;
6555
};
6656
}
6757

@@ -83,6 +73,12 @@ declare module '@ioc:EidelLev/Inertia' {
8373
manifestFile: (path: string) => string;
8474
}
8575

76+
export interface SsrRenderResult {
77+
head: string[];
78+
body: string;
79+
}
80+
8681
const Inertia: InertiaGlobal;
82+
8783
export default Inertia;
8884
}

providers/InertiaProvider/InertiaProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class InertiaProvider {
3737
}
3838

3939
/**
40-
* Register the `inertia` view global
40+
* Register the `inertiaHead` view global
4141
*/
4242
private registerInertiaHeadViewGlobal(View: ViewContract) {
4343
View.global('inertiaHead', (page: Record<string, unknown>) => {

src/Inertia.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
SharedData,
99
Version,
1010
VersionValue,
11+
SsrRenderResult,
1112
} from '@ioc:EidelLev/Inertia';
1213
import { readFile } from 'fs/promises';
1314
import md5 from 'md5';
@@ -95,6 +96,7 @@ export class Inertia implements InertiaContract {
9596
// Initial page render in SSR mode
9697
if (ssr.enabled) {
9798
const { head, body } = await this.renderSsrPage(page);
99+
98100
return view.render(inertiaView, {
99101
page: {
100102
ssrHead: head,
@@ -108,28 +110,10 @@ export class Inertia implements InertiaContract {
108110
return view.render(inertiaView, { page, ...pageOnlyProps });
109111
}
110112

111-
private async renderSsrPage(page: any): Promise<{ head: string[]; body: string }> {
112-
const { ssr } = this.config;
113-
const { mode, pageRootDir = 'js/Pages' } = ssr || {};
114-
115-
if (!mode) {
116-
throw new Error('No SSR mode was selected');
117-
}
118-
119-
if (mode === 'react') {
120-
const React = await import('react');
121-
const ReactDOMServer = await import('react-dom/server');
122-
const { createInertiaApp } = await import('@inertiajs/inertia-react');
113+
private renderSsrPage(page: any): Promise<SsrRenderResult> {
114+
const render = require(this.app.publicPath('ssr', 'ssr.js')).default;
123115

124-
return createInertiaApp<ResponseProps>({
125-
resolve: (name: string) => require(this.app.resourcesPath(pageRootDir, name)).default,
126-
render: ReactDOMServer.renderToString,
127-
page,
128-
setup: ({ App, props }) => React.createElement(App, props),
129-
});
130-
}
131-
132-
throw new Error(`SSR mode for '${mode}' is currently not supported`);
116+
return render(page);
133117
}
134118

135119
/**
@@ -192,6 +176,7 @@ export class Inertia implements InertiaContract {
192176
// Marshall back into an object
193177
return Object.fromEntries(result);
194178
}
179+
195180
/**
196181
* Simply replace with Adonis' `response.redirect().withQs().back()`
197182
*/

0 commit comments

Comments
 (0)