Skip to content

Commit

Permalink
fix(project): integration of prerender
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Nov 29, 2019
1 parent 9570198 commit ff9f87e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"build:prod": "ng build --prod",
"build:client-and-server-bundles": "ng build --prod && ng run ngx-linkifyjs-demo:server:production --bundleDependencies all",
"build:client-and-server-bundles:ghpages": "npm run build:prod:ghpages && ng run ngx-linkifyjs-demo:server:production --bundleDependencies all",
"build:docs": "npx compodoc -p projects/ngx-linkifyjs/tsconfig.lib.json --hideGenerator --disableCoverage -d dist/browser/doc/",
"build:prod:ghpages": "ng build --preserve-symlinks --prod --aot --build-optimizer --base-href /ngx-linkifyjs/ --deploy-url /ngx-linkifyjs/",
"build:prerender": "npm run build:client-and-server-bundles && npm run compile:server && npm run generate:prerender",
"build:prerender:ghpages": "npm run build:client-and-server-bundles:ghpages && npm run compile:server && npm run generate:prerender",
"deploy": "ng deploy --base-href=/ngx-linkifyjs/ --no-build",
"generate:prerender": "cd dist && node prerender",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"compile:server": "webpack --config webpack.server.config.js --progress --colors",
"serve:prerender": "cd dist/browser && http-server",
"serve:ssr": "node dist/server",
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
"build:client-and-server-bundles": "ng build --prod && ng run ngx-linkifyjs-demo:server:production"
"release": "release-it",
"test:watch": "jest --watch"
},
"private": true,
"dependencies": {
Expand Down
48 changes: 48 additions & 0 deletions prerender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Load zone.js for the server.
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

import { enableProdMode } from '@angular/core';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import { renderModuleFactory } from '@angular/platform-server';
import { ROUTES } from './static.paths';

// (global as any).WebSocket = require('ws');
// (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

const BROWSER_FOLDER = join(process.cwd(), 'browser');

// Load the index.html file containing referances to your application bundle.
const index = readFileSync(join(BROWSER_FOLDER, 'index.html'), 'utf8');

let previousRender = Promise.resolve();

// Iterate each route path
ROUTES.forEach(route => {
const fullPath = join(BROWSER_FOLDER, route);

// Make sure the directory structure is there
if (!existsSync(fullPath)) {
mkdirSync(fullPath);
}

// Writes rendered HTML to index.html, replacing the file if it already exists.
previousRender = previousRender
.then(_ =>
renderModuleFactory(AppServerModuleNgFactory, {
document: index,
url: route,
extraProviders: [provideModuleMap(LAZY_MODULE_MAP)]
})
)
.then(html => writeFileSync(join(fullPath, 'index.html'), html));
});
1 change: 0 additions & 1 deletion webpack.config.js

This file was deleted.

0 comments on commit ff9f87e

Please sign in to comment.