Skip to content

Commit

Permalink
feat(vite-plugin-nitro): provide req and res to renderer
Browse files Browse the repository at this point in the history
PR adds full req and res object from h3 to the analog event handler for  app rendering

closes analogjs#802
  • Loading branch information
Jefftopia committed Dec 18, 2023
1 parent 1c4f3fb commit b13c7bd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
9 changes: 7 additions & 2 deletions apps/analog-app/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { provideHttpClient } from '@angular/common/http';
import {
provideHttpClient,
withFetch,
withInterceptors,
} from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';
import { provideClientHydration } from '@angular/platform-browser';
import { provideFileRouter } from '@analogjs/router';
import { withNavigationErrorHandler } from '@angular/router';
import { cookieInterceotor } from './interceptors/cookies.interceptor';

export const appConfig: ApplicationConfig = {
providers: [
provideFileRouter(withNavigationErrorHandler(console.error)),
provideHttpClient(),
provideHttpClient(withFetch(), withInterceptors([cookieInterceotor])),
provideClientHydration(),
],
};
23 changes: 23 additions & 0 deletions apps/analog-app/src/app/interceptors/cookies.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isPlatformServer } from '@angular/common';
import { HttpHandlerFn, HttpHeaders, HttpRequest } from '@angular/common/http';
import { PLATFORM_ID, inject } from '@angular/core';

export function cookieInterceotor(
req: HttpRequest<unknown>,
next: HttpHandlerFn,
location = inject(PLATFORM_ID)
) {
if (isPlatformServer(location)) {
let headers = new HttpHeaders();
const cookies = req.headers.get('cookie');
headers = headers.set('cookie', cookies ?? '');

const cookiedRequest = req.clone({
headers,
});

return next(cookiedRequest);
} else {
return next(req);
}
}
16 changes: 14 additions & 2 deletions apps/analog-app/src/main.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'zone.js/node';
import { enableProdMode } from '@angular/core';
import { InjectionToken, enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { renderApplication } from '@angular/platform-server';

Expand All @@ -10,14 +10,26 @@ if (import.meta.env.PROD) {
enableProdMode();
}

const REQUEST = new InjectionToken<Request>('REQUEST');
const RESPONSE = new InjectionToken<Response>('RESPONSE');

export function bootstrap() {
return bootstrapApplication(AppComponent, config);
}

export default async function render(url: string, document: string) {
export default async function render(
url: string,
document: string,
req: Request,
res: Response
) {
const html = await renderApplication(bootstrap, {
document,
url,
platformProviders: [
{ provide: REQUEST, useValue: req },
{ provide: RESPONSE, useValue: res },
],
});

return html;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export function devServerPlugin(options: ServerOptions): Plugin {
const entryServer = (
await viteServer.ssrLoadModule('~analog/entry-server')
)['default'];
const result = await entryServer(req.originalUrl, template);
const result = await entryServer(
req.originalUrl,
template,
req,
res
);
res.setHeader('Content-Type', 'text/html');
res.end(result);
} catch (e) {
Expand Down
8 changes: 6 additions & 2 deletions packages/vite-plugin-nitro/src/lib/runtime/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import renderer from '#analog/ssr';
import template from '#analog/index';

export default eventHandler(async (event) => {
const html = await renderer(event.node.req.url, template);

const html = await renderer(
event.node.req.url,
template,
event.node.req,
event.node.res
);
return html;
});

0 comments on commit b13c7bd

Please sign in to comment.