diff --git a/angular.json b/angular.json index 3ecefba..9392cbb 100644 --- a/angular.json +++ b/angular.json @@ -40,11 +40,8 @@ "src/styles/main.scss" ], "scripts": [], - "server": "src/main.server.ts", - "prerender": true, - "ssr": { - "entry": "server.ts" - } + "prerender": false, + "ssr": false }, "configurations": { "production": { @@ -67,13 +64,6 @@ "optimization": false, "extractLicenses": false, "sourceMap": true - }, - "dev-spa": { - "optimization": false, - "extractLicenses": false, - "sourceMap": true, - "ssr": false, - "prerender": false } }, "defaultConfiguration": "production" @@ -86,9 +76,6 @@ }, "development": { "buildTarget": "why-app:build:development" - }, - "dev-spa": { - "buildTarget": "why-app:build:dev-spa" } }, "defaultConfiguration": "development" diff --git a/package.json b/package.json index 843f779..94d322b 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "type": "module", "scripts": { "preinstall": "npx only-allow pnpm", - "dev": "ng serve -c dev-spa", + "dev": "ng serve -c development", "start": "ng serve -c development", "build": "ng build --aot --verbose", "watch": "ng build --watch -c development", diff --git a/server.ts b/server.ts deleted file mode 100644 index 7083b14..0000000 --- a/server.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { APP_BASE_HREF } from '@angular/common'; -import { CommonEngine } from '@angular/ssr'; -import express from 'express'; -import { fileURLToPath } from 'node:url'; -import { dirname, join, resolve } from 'node:path'; -import bootstrap from './src/main.server'; - -// 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'); - - 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; - - commonEngine - .render({ - bootstrap, - documentFilePath: indexHtml, - url: `${protocol}://${headers.host}${originalUrl}`, - publicPath: browserDistFolder, - providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }], - }) - .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(); diff --git a/src/app/app.config.server.ts b/src/app/app.config.server.ts deleted file mode 100644 index b4d57c9..0000000 --- a/src/app/app.config.server.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { mergeApplicationConfig, ApplicationConfig } from '@angular/core'; -import { provideServerRendering } from '@angular/platform-server'; -import { appConfig } from './app.config'; - -const serverConfig: ApplicationConfig = { - providers: [ - provideServerRendering() - ] -}; - -export const config = mergeApplicationConfig(appConfig, serverConfig); diff --git a/src/app/services/firestore.service.ts b/src/app/services/firestore.service.ts index a53dcd0..ccdbe0e 100644 --- a/src/app/services/firestore.service.ts +++ b/src/app/services/firestore.service.ts @@ -1,4 +1,4 @@ -import { Injectable, inject, isDevMode } from '@angular/core'; +import { inject, isDevMode } from '@angular/core'; import { Firestore, collection, @@ -54,7 +54,10 @@ export class FirestoreService { const query = this.createQuery(...constraints); return getDocs(query).then((snapshot) => { const result: T[] = []; - snapshot.forEach((doc) => result.push(doc.data(snapshotOptions))); + snapshot.forEach((doc) => result.push({ + id: doc.id, + ...doc.data(snapshotOptions) + })); return result; }); } diff --git a/src/app/services/user-data.service.ts b/src/app/services/user-data.service.ts index c7bc4c7..ddc3c13 100644 --- a/src/app/services/user-data.service.ts +++ b/src/app/services/user-data.service.ts @@ -1,4 +1,5 @@ -import { Injectable, Signal, computed, signal } from '@angular/core'; +import { isPlatformBrowser } from '@angular/common'; +import { Injectable, PLATFORM_ID, inject, signal } from '@angular/core'; export const localKey = 'user-data'; export const pageReadTime = '__page-read-in'; @@ -16,18 +17,21 @@ function startDownload(url: string, filename: string) { providedIn: 'root', }) export class UserDataService { + readonly platformId = inject(PLATFORM_ID); readonly userData = signal(this.load()); private load(): Record> { - const data = localStorage.getItem(localKey); - if (data) { - try { - return JSON.parse(data); - } catch (e) { - console.error(e); - } - } - return {}; + if (isPlatformBrowser(this.platformId)) { + const data = localStorage.getItem(localKey); + if (data) { + try { + return JSON.parse(data); + } catch (e) { + console.error(e); + } + } + } + return {}; } getEntry(id: number | string): Record { @@ -35,25 +39,36 @@ export class UserDataService { } save(id: number | string, data: Record) { - this.userData.update(obj => ({ - ...obj, - [id]: { - ...obj[id], - ...data - } - })); - localStorage.setItem(localKey, JSON.stringify(this.userData())); + if (isPlatformBrowser(this.platformId)) { + this.userData.update((obj) => ({ + ...obj, + [id]: { + ...obj[id], + ...data, + }, + })); + localStorage.setItem(localKey, JSON.stringify(this.userData())); + } else { + console.warn(`Cannot save ${id} in server`); + } } clear() { - this.userData.set({}); - localStorage.removeItem(localKey); + if (isPlatformBrowser(this.platformId)) { + this.userData.set({}); + localStorage.removeItem(localKey); + } } download() { - const json = JSON.stringify(this.userData()); - const blob = new Blob([json], { type: 'application/json' }); - const url = URL.createObjectURL(blob); - startDownload(url, 'user-data.json'); + const data = this.userData(); + if (data) { + const json = JSON.stringify(data); + const blob = new Blob([json], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + startDownload(url, 'user-data.json'); + } else { + console.info('No data to download'); + } } } diff --git a/src/main.server.ts b/src/main.server.ts deleted file mode 100644 index 63298b1..0000000 --- a/src/main.server.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { bootstrapApplication } from '@angular/platform-browser'; -import { AppComponent } from './app/components/app.component'; -import { config } from './app/app.config.server'; - -const bootstrap = () => bootstrapApplication(AppComponent, config); - -export default bootstrap; diff --git a/tsconfig.app.json b/tsconfig.app.json index 7dc7284..f2161bd 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -8,9 +8,7 @@ ] }, "files": [ - "src/main.ts", - "src/main.server.ts", - "server.ts" + "src/main.ts" ], "include": [ "src/**/*.d.ts"