Skip to content

Commit

Permalink
fix(store): do not re-use the global Store instance between differe…
Browse files Browse the repository at this point in the history
…nt apps (ngxs#1740)
  • Loading branch information
arturovt authored May 2, 2021
1 parent d0a8d95 commit 60ff1ae
Show file tree
Hide file tree
Showing 22 changed files with 679 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $ npm install @ngxs/store@dev
```

- Fix: Allow to inject the `Store` into the custom error handler [#1708](https://github.com/ngxs/store/pull/1708)
- Fix: Do not re-use the global `Store` instance between different apps [#1740](https://github.com/ngxs/store/pull/1740)
- Performance: Tree-shake errors and warnings [#1732](https://github.com/ngxs/store/pull/1732)
- Performance: Router Plugin - Tree-shake `isAngularInTestMode()` [#1738](https://github.com/ngxs/store/pull/1738)
- Performance: Tree-shake `isAngularInTestMode()` [#1739](https://github.com/ngxs/store/pull/1739)
Expand Down
8 changes: 4 additions & 4 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"path": "./@ngxs/store/fesm2015/ngxs-store-internals.js",
"package": "@ngxs/store/internals",
"target": "es2015",
"maxSize": "6.220KB",
"maxSize": "10.85KB",
"compression": "none"
},
{
"path": "./@ngxs/store/fesm5/ngxs-store-internals.js",
"package": "@ngxs/store/internals",
"target": "es5",
"maxSize": "7.180KB",
"maxSize": "11.80KB",
"compression": "none"
},
{
Expand All @@ -32,14 +32,14 @@
"path": "./@ngxs/store/fesm2015/ngxs-store.js",
"package": "@ngxs/store",
"target": "es2015",
"maxSize": "116.30KB",
"maxSize": "116.45KB",
"compression": "none"
},
{
"path": "./@ngxs/store/fesm5/ngxs-store.js",
"package": "@ngxs/store",
"target": "es5",
"maxSize": "135.86KB",
"maxSize": "135.95KB",
"compression": "none"
},
{
Expand Down
23 changes: 20 additions & 3 deletions integrations/hello-world-ng11-ivy/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,27 @@
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"browserTarget": "hello-world-ng11-ivy:build"
"outputPath": "dist-integration-server",
"main": "server.ts",
"tsConfig": "tsconfig.server.json"
},
"configurations": {
"production": {
"optimization": true,
"outputHashing": "none",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
},
"test": {
Expand Down
2 changes: 1 addition & 1 deletion integrations/hello-world-ng11-ivy/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"path": "./dist-integration/main.*.js",
"target": "es2015",
"maxSize": "251.05 kB",
"maxSize": "251.86 kB",
"compression": "none"
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference types="cypress" />

describe('Select decorator returning state from the wrong store during SSR (https://github.com/ngxs/store/issues/1646)', () => {
it('should make concurrent requests and app should render correctly for each request', async () => {
const promises: Promise<string>[] = Array.from({ length: 100 }).map(() =>
fetch('/').then(res => res.text())
);

const bodies = await Promise.all(promises);

bodies.forEach(body => {
expect(body).to.contain('Angular 11 (ivy) Integration Test');
expect(body).to.contain('Counter is 0');
});
});
});
12 changes: 9 additions & 3 deletions integrations/hello-world-ng11-ivy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
"start": "ng serve",
"build": "ng build",
"build:prod": "ng build --prod",
"build:ssr:prod": "yarn build:prod && yarn ng run hello-world-ng11-ivy:server:production",
"test": "ng test",
"test:ci": "ng test --run-in-band --silent",
"test:integration": "yarn install:ivy && yarn test:ci && yarn build:prod && yarn bundlesize && yarn e2e:ci",
"test:integration": "yarn install:ivy && yarn test:ci && yarn build:ssr:prod && yarn bundlesize && yarn e2e:ci && yarn e2e:ci:ssr",
"preinstall": "yarn --cwd ../../ cpx -v -C \"@ngxs/**/*\" integrations/hello-world-ng11-ivy/node_modules/@ngxs",
"install:ivy": "yarn --frozen-lockfile --non-interactive --no-progress",
"postinstall": "yarn ngcc --async false",
"cy:open": "cypress open --config integrationFolder=cypress/integration",
"cy:run": "cypress run --config integrationFolder=cypress/integration",
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:run:ssr": "cypress run --config integrationFolder=cypress/integration-ssr",
"serve:integration:static": "angular-http-server --path dist-integration -p 4200 --silent",
"serve:integration:ssr": "node dist-integration-server/main",
"e2e": "start-test serve:integration:static 4200 cy:open",
"e2e:ci": "start-test serve:integration:static 4200 cy:run",
"e2e:ci:ssr": "start-test serve:integration:ssr 4200 cy:run:ssr",
"bundlesize": "bundlesize --config bundlesize.config.json"
},
"private": true,
Expand All @@ -29,7 +33,9 @@
"@angular/forms": "~11.2.4",
"@angular/platform-browser": "~11.2.4",
"@angular/platform-browser-dynamic": "~11.2.4",
"@angular/platform-server": "~11.2.4",
"@angular/router": "~11.2.4",
"@nguniversal/express-engine": "~11.2.1",
"@ngxs/devtools-plugin": "file:./node_modules/@ngxs/devtools-plugin",
"@ngxs/form-plugin": "file:./node_modules/@ngxs/form-plugin",
"@ngxs/logger-plugin": "file:./node_modules/@ngxs/logger-plugin",
Expand Down
67 changes: 67 additions & 0 deletions integrations/hello-world-ng11-ivy/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'zone.js/dist/zone-node';

import { APP_BASE_HREF } from '@angular/common';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import * as express from 'express';
import { join } from 'path';

import { AppServerModule } from './src/main.server';

export function app() {
const server = express();
const distFolder = join(process.cwd(), 'dist-integration');

server.engine(
'html',
ngExpressEngine({
bootstrap: AppServerModule
}) as any
);

server.set('view engine', 'html');
server.set('views', distFolder);

server.get(
'*.*',
express.static(distFolder, {
maxAge: '1y'
})
);

server.get('*', (req, res) => {
res.render('index', {
req,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: REQUEST, useValue: req },
{ provide: RESPONSE, useValue: res }
]
});
});

return server;
}

function run() {
const port = process.env.PORT || 4200;

const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
// eslint-disable-next-line
declare const __non_webpack_require__: NodeRequire;
// eslint-disable-next-line
const mainModule = __non_webpack_require__.main;
const moduleFilename = (mainModule && mainModule.filename) || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}

export * from './src/main.server';
8 changes: 6 additions & 2 deletions integrations/hello-world-ng11-ivy/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { AppComponent } from './app.component';
import { StoreModule } from './store/store.module';

const routes: Routes = [{ path: '', component: AppComponent }];

@NgModule({
imports: [
BrowserModule.withServerTransition({ appId: 'hello-world-ng11-ivy' }),
RouterModule.forRoot(routes),
StoreModule
],
declarations: [AppComponent],
imports: [BrowserModule, RouterModule.forRoot(routes), StoreModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
11 changes: 11 additions & 0 deletions integrations/hello-world-ng11-ivy/src/app/app.server.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
imports: [AppModule, ServerModule],
bootstrap: [AppComponent]
})
export class AppServerModule {}
Loading

0 comments on commit 60ff1ae

Please sign in to comment.