-
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.
Browse files
Browse the repository at this point in the history
setting up angular universal for ssr
- Loading branch information
Showing
10 changed files
with
241 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,52 @@ | ||
{ | ||
"hosting": { | ||
"public": "dist/dahabguide", | ||
"public": "dist/browser", | ||
"ignore": [ | ||
"firebase.json", | ||
"**/.*", | ||
"**/node_modules/**" | ||
], | ||
"rewrites": [{ | ||
"source": "**", | ||
"destination": "/index.html" | ||
}] | ||
"function": "ssr" | ||
}], | ||
"headers": [{ | ||
"source": "**/*.@(jpg|jpeg|gif|png|svg)", | ||
"headers": [{ | ||
"key": "Cache-Control", | ||
"value": "max-age=86400" | ||
}, | ||
{ | ||
"key": "Content-Encoding", | ||
"value": "gzip" | ||
} | ||
] | ||
}, | ||
{ | ||
"source": "**/*.@(js|css)", | ||
"headers": [{ | ||
"key": "Cache-Control", | ||
"value": "max-age=31536000" | ||
}, | ||
{ | ||
"key": "Content-Encoding", | ||
"value": "gzip" | ||
} | ||
] | ||
}, | ||
{ | ||
"source": "**/*-sw.js", | ||
"headers": [{ | ||
"key": "Cache-Control", | ||
"value": "no-cache" | ||
}] | ||
} | ||
] | ||
}, | ||
"functions": { | ||
"predeploy": [ | ||
"npm --prefix \"$RESOURCE_DIR\" run lint", | ||
"npm --prefix \"$RESOURCE_DIR\" run build" | ||
] | ||
} | ||
} |
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,61 @@ | ||
/** | ||
* *** NOTE ON IMPORTING FROM ANGULAR AND NGUNIVERSAL IN THIS FILE *** | ||
* | ||
* If your application uses third-party dependencies, you'll need to | ||
* either use Webpack or the Angular CLI's `bundleDependencies` feature | ||
* in order to adequately package them for use on the server without a | ||
* node_modules directory. | ||
* | ||
* However, due to the nature of the CLI's `bundleDependencies`, importing | ||
* Angular in this file will create a different instance of Angular than | ||
* the version in the compiled application code. This leads to unavoidable | ||
* conflicts. Therefore, please do not explicitly import from @angular or | ||
* @nguniversal in this file. You can export any needed resources | ||
* from your application's main.server.ts file, as seen below with the | ||
* import for `ngExpressEngine`. | ||
*/ | ||
|
||
(global as any).WebSocket = require('ws'); | ||
(global as any).XMLHttpRequest = require('xhr2'); | ||
|
||
import 'zone.js/dist/zone-node'; | ||
|
||
import * as express from 'express'; | ||
import { join } from 'path'; | ||
|
||
// Express server | ||
const app = express(); | ||
|
||
const PORT = process.env.PORT || 4000; | ||
const DIST_FOLDER = join(process.cwd(), 'dist/browser'); | ||
|
||
// * NOTE :: leave this as require() since this file is built Dynamically from webpack | ||
const { AppServerModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap } = require('./dist/server/main'); | ||
|
||
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) | ||
app.engine('html', ngExpressEngine({ | ||
bootstrap: AppServerModuleNgFactory, | ||
providers: [ | ||
provideModuleMap(LAZY_MODULE_MAP) | ||
] | ||
})); | ||
|
||
app.set('view engine', 'html'); | ||
app.set('views', DIST_FOLDER); | ||
|
||
// Example Express Rest API endpoints | ||
// app.get('/api/**', (req, res) => { }); | ||
// Serve static files from /browser | ||
app.get('*.*', express.static(DIST_FOLDER, { | ||
maxAge: '1y' | ||
})); | ||
|
||
// All regular routes use the Universal engine | ||
app.get('*', (req, res) => { | ||
res.render('index', { req }); | ||
}); | ||
|
||
// Start up the Node server | ||
app.listen(PORT, () => { | ||
console.log(`Node Express server listening on http://localhost:${PORT}`); | ||
}); |
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,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { ServerModule } from '@angular/platform-server'; | ||
|
||
import { AppModule } from './app.module'; | ||
import { AppComponent } from './app.component'; | ||
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
AppModule, | ||
ServerModule, | ||
ModuleMapLoaderModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppServerModule {} |
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,12 @@ | ||
import { enableProdMode } from '@angular/core'; | ||
|
||
import { environment } from './environments/environment'; | ||
|
||
if (environment.production) { | ||
enableProdMode(); | ||
} | ||
|
||
export { AppServerModule } from './app/app.server.module'; | ||
export { ngExpressEngine } from '@nguniversal/express-engine'; | ||
export { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; | ||
|
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,13 @@ | ||
{ | ||
"extends": "./tsconfig.app.json", | ||
"compilerOptions": { | ||
"outDir": "./out-tsc/app-server", | ||
"module": "commonjs" | ||
}, | ||
"files": [ | ||
"src/main.server.ts" | ||
], | ||
"angularCompilerOptions": { | ||
"entryModule": "./src/app/app.server.module#AppServerModule" | ||
} | ||
} |
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,56 @@ | ||
// Work around for https://github.com/angular/angular-cli/issues/7200 | ||
|
||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
|
||
module.exports = { | ||
mode: 'none', | ||
entry: { | ||
// This is our Express server for Dynamic universal | ||
server: './server.ts' | ||
}, | ||
externals: { | ||
'./dist/server/main': 'require("./server/main")' | ||
}, | ||
target: 'node', | ||
resolve: { | ||
extensions: ['.ts', '.js'] | ||
}, | ||
optimization: { | ||
minimize: false | ||
}, | ||
output: { | ||
// Puts the output at the root of the dist folder | ||
path: path.join(__dirname, 'dist'), | ||
filename: '[name].js' | ||
}, | ||
module: { | ||
noParse: /polyfills-.*\.js/, | ||
rules: [{ | ||
test: /\.ts$/, | ||
loader: 'ts-loader' | ||
}, | ||
{ | ||
// Mark files inside `@angular/core` as using SystemJS style dynamic imports. | ||
// Removing this will cause deprecation warnings to appear. | ||
test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/, | ||
parser: { | ||
system: true | ||
}, | ||
}, | ||
] | ||
}, | ||
plugins: [ | ||
new webpack.ContextReplacementPlugin( | ||
// fixes WARNING Critical dependency: the request of a dependency is an expression | ||
/(.+)?angular(\\|\/)core(.+)?/, | ||
path.join(__dirname, 'src'), // location of your src | ||
{} // a map of your routes | ||
), | ||
new webpack.ContextReplacementPlugin( | ||
// fixes WARNING Critical dependency: the request of a dependency is an expression | ||
/(.+)?express(\\|\/)(.+)?/, | ||
path.join(__dirname, 'src'), {} | ||
) | ||
] | ||
}; |