-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
176 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { CommonEngine } from '@angular/ssr'; | ||
import express from 'express'; | ||
import cookieParser from 'cookie-parser'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { dirname, join, resolve } from 'node:path'; | ||
import bootstrap from './src/main.server'; | ||
import { REQUEST, RESPONSE } from './src/app/ssr.tokens'; | ||
|
||
// The Express app is exported so that it can be used by serverless Functions. | ||
export function app(): express.Express { | ||
const server = express(); | ||
const serverDistFolder = dirname(fileURLToPath(import.meta.url)); | ||
const browserDistFolder = resolve(serverDistFolder, '../browser'); | ||
const indexHtml = join(serverDistFolder, 'index.server.html'); | ||
|
||
server.use(cookieParser()); | ||
|
||
const commonEngine = new CommonEngine(); | ||
|
||
server.set('view engine', 'html'); | ||
server.set('views', browserDistFolder); | ||
|
||
// Example Express Rest API endpoints | ||
// server.get('/api/**', (req, res) => { }); | ||
// Serve static files from /browser | ||
server.get( | ||
'*.*', | ||
express.static(browserDistFolder, { | ||
maxAge: '1y', | ||
}), | ||
); | ||
|
||
// All regular routes use the Angular engine | ||
server.get('*', (req, res, next) => { | ||
const { protocol, originalUrl, baseUrl, headers } = req; | ||
|
||
// The server.ts not used for ng serve, so REQUEST and RESPONSE tokens won't be set | ||
// https://github.com/angular/angular-cli/issues/26323 | ||
|
||
commonEngine | ||
.render({ | ||
bootstrap, | ||
documentFilePath: indexHtml, | ||
url: `${protocol}://${headers.host}${originalUrl}`, | ||
publicPath: browserDistFolder, | ||
providers: [ | ||
{ provide: APP_BASE_HREF, useValue: baseUrl }, | ||
{ provide: REQUEST, useValue: req }, | ||
{ provide: RESPONSE, useValue: res }, | ||
], | ||
}) | ||
.then((html) => res.send(html)) | ||
.catch((err) => next(err)); | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
// function run(): void { | ||
// const port = process.env['PORT'] || 4000; | ||
// | ||
// // Start up the Node server | ||
// const server = app(); | ||
// server.listen(port, () => { | ||
// console.log(`Node Express server listening on http://localhost:${port}`); | ||
// }); | ||
// } | ||
// | ||
// run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ApplicationConfig, mergeApplicationConfig } from '@angular/core'; | ||
import { provideServerRendering } from '@angular/platform-server'; | ||
import { appConfig } from './app.config'; | ||
import { ServerSessionStorage } from './spotify-client/server-session-storage.service'; | ||
import { SessionStorage } from './spotify-client/session-store.service'; | ||
|
||
const serverConfig: ApplicationConfig = { | ||
providers: [ | ||
provideServerRendering(), | ||
{ | ||
provide: SessionStorage, | ||
useClass: ServerSessionStorage, | ||
}, | ||
], | ||
}; | ||
|
||
export const config = mergeApplicationConfig(appConfig, serverConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
import { ApplicationConfig } from '@angular/core'; | ||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; | ||
import { provideRouter, withRouterConfig } from '@angular/router'; | ||
import { | ||
provideRouter, | ||
withEnabledBlockingInitialNavigation, | ||
withRouterConfig, | ||
} from '@angular/router'; | ||
import { routes } from './app.routes'; | ||
import { provideHttpClient, withInterceptors } from '@angular/common/http'; | ||
import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http'; | ||
import { authenticationInterceptor } from './spotify-client/authentication.interceptor'; | ||
import { BrowserSessionStorage } from './spotify-client/browser-session-storage.service'; | ||
import { SessionStorage } from './spotify-client/session-store.service'; | ||
import { provideClientHydration } from '@angular/platform-browser'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideAnimationsAsync(), | ||
provideRouter(routes, withRouterConfig({ onSameUrlNavigation: 'reload' })), | ||
provideHttpClient(withInterceptors([authenticationInterceptor])), | ||
provideRouter( | ||
routes, | ||
withRouterConfig({ onSameUrlNavigation: 'reload' }), | ||
withEnabledBlockingInitialNavigation(), | ||
), | ||
provideHttpClient(withFetch(), withInterceptors([authenticationInterceptor])), | ||
{ | ||
provide: SessionStorage, | ||
useClass: BrowserSessionStorage, | ||
}, | ||
provideClientHydration(), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { REQUEST, RESPONSE } from '../ssr.tokens'; | ||
import { SessionStorage } from './session-store.service'; | ||
|
||
// Firebase hosting only allows the `__session` cookie | ||
// https://firebase.google.com/docs/hosting/manage-cache#using_cookies | ||
const sessionCookieKey = '__session'; | ||
|
||
@Injectable() | ||
export class ServerSessionStorage extends SessionStorage { | ||
// use "optional: true" to work with pre rendering and ng serve | ||
// https://github.com/angular/angular-cli/issues/26323 | ||
private readonly request = inject(REQUEST, { optional: true }); | ||
private readonly response = inject(RESPONSE, { optional: true }); | ||
|
||
public getSessionData(): Record<string, string> | null { | ||
const sessionCookie = this.request?.cookies[sessionCookieKey]; | ||
if (!sessionCookie) { | ||
return null; | ||
} | ||
return JSON.parse(sessionCookie); | ||
} | ||
|
||
public saveSessionData(sessionCookie: Record<string, string>) { | ||
if (Object.keys(sessionCookie).length === 0) { | ||
this.response?.clearCookie(sessionCookieKey); | ||
} else { | ||
this.response?.cookie(sessionCookieKey, JSON.stringify(sessionCookie), { maxAge: 604800000 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { Request, Response } from 'express'; | ||
|
||
// https://github.com/angular/angular-cli/issues/26110 | ||
export const REQUEST: InjectionToken<Request> = new InjectionToken<Request>('REQUEST'); | ||
export const RESPONSE: InjectionToken<Response> = new InjectionToken<Response>('RESPONSE'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { AppComponent } from './app/app.component'; | ||
import { config } from './app/app.config.server'; | ||
|
||
const bootstrap = () => bootstrapApplication(AppComponent, config); | ||
|
||
export default bootstrap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters