Skip to content

Commit

Permalink
Pure SPA: disabled prerender + ssr to fix firestore caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ortwic committed Apr 4, 2024
1 parent a2d362d commit e8411a5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 119 deletions.
17 changes: 2 additions & 15 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -67,13 +64,6 @@
"optimization": false,
"extractLicenses": false,
"sourceMap": true
},
"dev-spa": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true,
"ssr": false,
"prerender": false
}
},
"defaultConfiguration": "production"
Expand All @@ -86,9 +76,6 @@
},
"development": {
"buildTarget": "why-app:build:development"
},
"dev-spa": {
"buildTarget": "why-app:build:dev-spa"
}
},
"defaultConfiguration": "development"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 0 additions & 56 deletions server.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/app/app.config.server.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/app/services/firestore.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, inject, isDevMode } from '@angular/core';
import { inject, isDevMode } from '@angular/core';
import {
Firestore,
collection,
Expand Down Expand Up @@ -54,7 +54,10 @@ export class FirestoreService {
const query = this.createQuery<T>(...constraints);
return getDocs<T, DocumentData>(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;
});
}
Expand Down
63 changes: 39 additions & 24 deletions src/app/services/user-data.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,44 +17,58 @@ function startDownload(url: string, filename: string) {
providedIn: 'root',
})
export class UserDataService<T = unknown> {
readonly platformId = inject(PLATFORM_ID);
readonly userData = signal(this.load());

private load(): Record<string, Record<string, T>> {
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<string, T> {
return this.userData()[id] ?? {};
}

save(id: number | string, data: Record<string, T>) {
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');
}
}
}
7 changes: 0 additions & 7 deletions src/main.server.ts

This file was deleted.

4 changes: 1 addition & 3 deletions tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
]
},
"files": [
"src/main.ts",
"src/main.server.ts",
"server.ts"
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
Expand Down

0 comments on commit e8411a5

Please sign in to comment.