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

feat(vite-plugin-nitro): provide req and res to renderer #806

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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';
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved

export const appConfig: ApplicationConfig = {
providers: [
provideFileRouter(withNavigationErrorHandler(console.error)),
provideHttpClient(),
provideHttpClient(withFetch(), withInterceptors([cookieInterceotor])),
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
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(
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
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
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
) {
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
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
);
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
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
);
return html;
});