We use index.php as the starting point, with angular.json (i've omitted irrelevent bits)
"projects": {
"angular": {
"architect": {
"build": {
"options": {
"outputPath": "dist",
"index": "src/index.php",
Prior to ng 12 this worked fine but ng 12 started commenting out the PHP code, until we used:
"optimization": {
"fonts": true,
"scripts": true,
"styles": {
"inlineCritical": false,
"minify": true
}
},
The reason we run PHP code is to preload data, based on the angular route, to save one round-trip back to the server to get the data. To explain further, normal latency in loading an angular page has these consecutive stages:
- load index.html (network latency)
- load JS files (network latency)
- JS loads data to display the page (network and API latency).
By preloading the data, we eliminate the network latency from 3. and the API calls are processed during step 1, while loading index.php.
There is a bit more to this, our Apache Virtual Server does a URL rewrite using (I've omitted some bits to keep this simple)
RewriteCond %{DOCUMENT_ROOT}/yii2/web/angular/dist/$1 -f
RewriteRule ^/app/(.*)$ /yii2/web/angular/dist/$1 [L]
RewriteRule ^/app/(.*)$ /yii2/web/angular/dist/index.php?pathroute=$1 [L]
When index.php runs it recovers the pathroute from the queryString and uses it to set up the preloading of data. The browser URL remains unchanged (this is not a 301 redirect, it's an internal URL rewrite).
If you access this page via the angular router from elsewhere in the app, the module lazy loads and the JS runs the API calls since the data is not preloaded.
As a result we have lightening fast loading of deep links.
The preloaded data plays in with our state manager that caches data from API calls, so the application code doesn't know where the data came from. The preload looks like this:
<script type="text/javascript">
_preload = {"params":{"accessLevel":"P","releasetag":"NDOW-BLD6BA/release20211001_BLD6BA_1","user":{"isAuthenticated":false,"updatei...