-
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
13 changed files
with
222 additions
and
22 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,22 @@ | ||
import { ApplicationConfig, mergeApplicationConfig } from '@angular/core'; | ||
import { provideServerRendering } from '@angular/platform-server'; | ||
import { appConfig } from './app.config'; | ||
import { ServerCacheStoreService } from './spotify-client/server-cache-store.service'; | ||
import { CACHE_STORE_TOKEN } from './spotify-client/spotify-client.service'; | ||
import { mockResizeObserverFactory, RESIZE_OBSERVER_FACTORY } from './shared/resize-observer'; | ||
|
||
const serverConfig: ApplicationConfig = { | ||
providers: [ | ||
provideServerRendering(), | ||
{ | ||
provide: CACHE_STORE_TOKEN, | ||
useClass: ServerCacheStoreService, | ||
}, | ||
{ | ||
provide: RESIZE_OBSERVER_FACTORY, | ||
useValue: mockResizeObserverFactory, | ||
}, | ||
], | ||
}; | ||
|
||
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,23 +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 { CACHE_STORE_TOKEN } from './spotify-client/spotify-client.service'; | ||
import { BrowserCacheStoreService } from './spotify-client/browser-cache-store.service'; | ||
import { provideClientHydration } from '@angular/platform-browser'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideAnimationsAsync(), | ||
provideRouter( | ||
routes, | ||
withRouterConfig({ onSameUrlNavigation: 'reload' }), | ||
withEnabledBlockingInitialNavigation(), | ||
), | ||
provideHttpClient(withInterceptors([authenticationInterceptor])), | ||
provideHttpClient(withFetch(), withInterceptors([authenticationInterceptor])), | ||
{ | ||
provide: CACHE_STORE_TOKEN, | ||
useClass: BrowserCacheStoreService, | ||
}, | ||
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
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,21 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export type ResizeObserverFactory = (callback: ResizeObserverCallback) => ResizeObserver; | ||
|
||
const browserResizeObserverFactory: ResizeObserverFactory = (callback) => | ||
new ResizeObserver(callback); | ||
|
||
export const RESIZE_OBSERVER_FACTORY = new InjectionToken<ResizeObserverFactory>( | ||
'ResizeObserverFactory', | ||
{ | ||
factory: () => browserResizeObserverFactory, | ||
}, | ||
); | ||
|
||
export const mockResizeObserverFactory: ResizeObserverFactory = () => mockResizeObserver; | ||
|
||
const mockResizeObserver: ResizeObserver = { | ||
observe() {}, | ||
disconnect() {}, | ||
unobserve() {}, | ||
}; |
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,49 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { ICacheStore } from '@spotify/web-api-ts-sdk/src/caching/ICacheStore'; | ||
import { REQUEST, RESPONSE } from '../ssr.tokens'; | ||
|
||
// Firebase hosting only allows the `__session` cookie | ||
// https://firebase.google.com/docs/hosting/manage-cache#using_cookies | ||
const sessionCookieKey = '__session'; | ||
|
||
@Injectable() | ||
export class ServerCacheStoreService implements ICacheStore { | ||
// 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 }); | ||
|
||
get(key: string): string | null { | ||
return this.getSessionCookie()?.[key] ?? null; | ||
} | ||
|
||
remove(key: string): void { | ||
const sessionCookie = this.getSessionCookie(); | ||
if (sessionCookie) { | ||
delete sessionCookie[key]; | ||
this.saveSessionCookie(sessionCookie); | ||
} | ||
} | ||
|
||
set(key: string, value: string): void { | ||
const sessionCookie = this.getSessionCookie() ?? {}; | ||
sessionCookie[key] = value; | ||
this.saveSessionCookie(sessionCookie); | ||
} | ||
|
||
private getSessionCookie(): Record<string, string> | null { | ||
const sessionCookie = this.request?.cookies[sessionCookieKey]; | ||
if (!sessionCookie) { | ||
return null; | ||
} | ||
return JSON.parse(sessionCookie); | ||
} | ||
|
||
private saveSessionCookie(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