Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit 6e191f6

Browse files
committed
♻️ add cache
1 parent 6e7e9ce commit 6e191f6

File tree

7 files changed

+54
-41
lines changed

7 files changed

+54
-41
lines changed

src/cache.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as path from 'path'
2+
const fs = require('fs-extra')
3+
4+
export class UniversalCache {
5+
private cacheFile: string = 'universal.cache.json'
6+
private cachePath: string = path.join(process.cwd(), this.cacheFile)
7+
8+
public setCache(data: any): Promise<any> {
9+
return fs.outputJson(this.cachePath, data)
10+
}
11+
12+
public getCache(): Promise<any> {
13+
try {
14+
const cache = fs.readJsonSync(this.cachePath)
15+
return Promise.resolve(cache)
16+
} catch (e) {
17+
return fs.outputJson(this.cachePath, {cache: true})
18+
.then(() => ({cache: true}))
19+
}
20+
}
21+
}

src/file.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { SiteGenConfig } from './interfaces'
22
import * as path from 'path'
33
const fs = require('fs-extra')
44

5-
65
export const createHTML = (html: string, url: string, config: SiteGenConfig) => {
76
let filePath = path.join(process.cwd(), config.outputPath)
87

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const prettysize = require('prettysize')
1010

1111
enableProdMode()
1212

13+
export { UniversalCache } from './cache'
14+
1315
export const generateSite = async (
1416
serverModuleOrFactory: Type<{}> | NgModuleFactory<{}>,
1517
document: string,
@@ -30,9 +32,13 @@ export const generateSite = async (
3032

3133
const pages = (configOptions.routes || await configOptions.getRoutes())
3234
.map((route: string) => {
33-
return Observable.fromPromise(renderPage(
34-
serverModuleOrFactory, document, route, configOptions
35-
))
35+
return Observable.of({serverModuleOrFactory, document, url: route, configOptions})
36+
.mergeMap((opts: any) => Observable.fromPromise(renderPage(
37+
opts.serverModuleOrFactory,
38+
opts.document,
39+
opts.url,
40+
opts.config
41+
)))
3642
})
3743

3844
const bar = new progressBar(' univeral building :url [:bar] :percent :total pages', {

src/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
import { Type, NgModuleFactory } from '@angular/core'
2+
13
export interface RouteFunc {
24
(): Promise<string[]>
35
}
46

7+
export interface RenderPageOpts {
8+
serverModuleOrFactory: Type<{}> | NgModuleFactory<{}>
9+
document: string
10+
url: string
11+
config: SiteGenConfig
12+
}
13+
514
export interface SiteGenConfig {
615
/** all your route paths that are registered the NgModule that you're rendering */
716
routes?: string[]

src/render.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import 'core-js'
22
import 'zone.js/dist/zone-node'
33
import { renderModule, renderModuleFactory } from '@angular/platform-server'
4-
import { NgModuleFactory, enableProdMode, Type } from '@angular/core'
4+
import { NgModuleFactory, Type, InjectionToken } from '@angular/core'
55
import { SiteGenConfig } from './interfaces'
6+
import { UniversalCache } from './cache'
67

7-
enableProdMode()
88

9+
export const CACHE = new InjectionToken('universal.cache')
910
/**
1011
* A thunk that takes a render method and later calls it with module or factory and platform options
11-
* @param renderMethod what method to render (rednerModule | renderModuleFactory)
12+
* @param renderMethod what method to render (renderModule | renderModuleFactory)
1213
*/
1314
const render = (renderMethod: Function) => {
14-
return function(moduleToRender, opts: {document: string, url: string}) {
15+
return function(moduleToRender, opts: {document: string, url: string, extraProviders?: any[]}) {
1516
return renderMethod.call(renderMethod, moduleToRender, opts)
1617
}
1718
}
@@ -22,6 +23,7 @@ const render = (renderMethod: Function) => {
2223
* @param document the index.html string
2324
* @param url the url to the page
2425
*/
26+
const cache = {data: []}
2527
export async function renderPage (
2628
serverModuleOrFactory: Type<{}> | NgModuleFactory<{}>,
2729
document: string,
@@ -40,7 +42,11 @@ export async function renderPage (
4042

4143
const html = await renderFunc(
4244
serverModuleOrFactory,
43-
{document, url}
45+
{
46+
document,
47+
url,
48+
extraProviders: [UniversalCache]
49+
}
4450
)
4551

4652
return {html, url}

webpack.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ module.exports = (envOptions = {}) => {
1212
entry: root('./src/index.ts'),
1313
output: {
1414
path: root('dist'),
15-
filename: 'universal-sitegen.js'
15+
filename: 'universal-sitegen.js',
16+
library: 'universal-sitegen',
17+
libraryTarget: 'umd',
18+
umdNamedDefine: true
1619
},
1720
target: 'node',
1821

webpack.server.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)