-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Next.js][Multi-site] Multi-site middleware plugin (#1279)
* jss-cli unit test coverage: first batch * jss-cli unit test coverage: second batch * jss-cli unit tests: second batch * fix expected output for test not to fail across environments * lint * dev-tools unit test coverage first batch * dev-tools unit test coverage: second batch * resolve-scjssconfig test placeholder (for the future!) * exclude index file from jss-vue test coverage (#1266) * version v21.1.0-canary.58 [skip ci] * fix test for scjssconfig resolving * Update packages/sitecore-jss-dev-tools/src/manifest/manifest-manager.test.ts Co-authored-by: Adam Brauer <400763+ambrauer@users.noreply.github.com> * Update packages/sitecore-jss-dev-tools/src/manifest/manifest-manager.test.ts Co-authored-by: Adam Brauer <400763+ambrauer@users.noreply.github.com> * lint * adding test data for scjssconfig * some improvements to reject logic in resolve scjssconfig * lint numero 2 * version v21.1.0-canary.59 [skip ci] * #556667: fixed urls for sitemap * version v21.1.0-canary.60 [skip ci] * final batch * re-gen yarn.lock * yarn.lock re-update * version v21.1.0-canary.61 [skip ci] * #552985: fixed header styles * version v21.1.0-canary.62 [skip ci] * #546298: fixed style for showing hidden components * version v21.1.0-canary.63 [skip ci] * SiteResolver.resolve updates: removed 'language' from site resolution logic, return SiteInfo instead of site name * [Next.js][Multi-site] Multi-site middleware plugin * #559044: fixed rendering dynamic placeholder (#1278) * version v21.1.0-canary.64 [skip ci] * Adjust with latest site resolver changes * adjust * Add latest changes * Extra comment * extra fix * Extend unit tests * Revert cookie set change * Use response cookies instead of request * Adjust changes according to review * Adjust changes according to review * lint fix Co-authored-by: Artem Alexeyenko <ala@sitecore.net> Co-authored-by: Automated Build <builds@sitecore.com> Co-authored-by: Adam Brauer <400763+ambrauer@users.noreply.github.com> Co-authored-by: Ruslan Matkovskyi <rusm@sitecore.net> Co-authored-by: Ruslan Matkovskyi <100142572+matkovskyi@users.noreply.github.com>
- Loading branch information
1 parent
5c2d3c0
commit bc26952
Showing
70 changed files
with
3,018 additions
and
228 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
37 changes: 37 additions & 0 deletions
37
...reate-sitecore-jss/src/templates/nextjs-multisite/src/lib/middleware/plugins/multisite.ts
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,37 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { MultisiteMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/middleware'; | ||
import { siteResolver } from 'lib/site-resolver'; | ||
import { MiddlewarePlugin } from '..'; | ||
|
||
/** | ||
* This is the multisite middleware plugin for Next.js. | ||
* It is used to enable Sitecore multisite in Next.js. | ||
* | ||
* The `MultisiteMiddleware` will | ||
* 1. Based on provided hostname and sites information, resolve site. | ||
* 2. Rewrite the response to the specific site. | ||
* 3. Set `sc_site` cookie with site name and `x-sc-rewrite` header with rewritten path to be reused in following middlewares. | ||
*/ | ||
class MultisitePlugin implements MiddlewarePlugin { | ||
private multisiteMiddleware: MultisiteMiddleware; | ||
|
||
// Multisite middleware has to be executed first | ||
order = -1; | ||
|
||
constructor() { | ||
this.multisiteMiddleware = new MultisiteMiddleware({ | ||
// This function determines if a route should be excluded from site resolution. | ||
// Certain paths are ignored by default (e.g. files and Next.js API routes), but you may wish to exclude more. | ||
// This is an important performance consideration since Next.js Edge middleware runs on every request. | ||
excludeRoute: () => false, | ||
// This function resolves site based on hostname | ||
getSite: siteResolver.getByHost, | ||
}); | ||
} | ||
|
||
async exec(req: NextRequest, res?: NextResponse): Promise<NextResponse> { | ||
return this.multisiteMiddleware.getHandler()(req, res); | ||
} | ||
} | ||
|
||
export const multisitePlugin = new MultisitePlugin(); |
2 changes: 1 addition & 1 deletion
2
packages/create-sitecore-jss/src/templates/nextjs-multisite/src/lib/site-resolver.ts
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
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 |
---|---|---|
|
@@ -4,6 +4,10 @@ | |
width: 100%; | ||
} | ||
|
||
.component { | ||
position: relative; | ||
} | ||
|
||
.container { | ||
padding: 0; | ||
|
||
|
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
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
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
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,73 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { getPackageScriptCommands, makeCommand } from './cli'; | ||
import * as resolvePkg from './resolve-package'; | ||
import * as packageScript from './run-package-script'; | ||
import { Arguments } from 'yargs'; | ||
|
||
describe('cli', () => { | ||
describe('getPackageScriptCommands', async () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
const packageJson = { | ||
scripts: { | ||
first: 'do --this', | ||
second: 'do --that', | ||
third: 'do --all', | ||
}, | ||
}; | ||
|
||
it('should read scripts from package.json and return result with handlers', async () => { | ||
sinon.stub(resolvePkg, 'default').resolves(packageJson); | ||
|
||
const result = await getPackageScriptCommands(); | ||
const runScriptStub = sinon.stub(packageScript, 'default'); | ||
const mockArgs: Arguments = { | ||
_: ['arg1', 'arg2'], | ||
$0: '', | ||
}; | ||
|
||
expect(Object.keys(packageJson.scripts)).to.be.deep.equal(Object.keys(result)); | ||
for (const key of Object.keys(result)) { | ||
const expectedCommand = makeCommand(key); | ||
for (const field of Object.keys(expectedCommand)) { | ||
if (typeof expectedCommand[field] === 'function') { | ||
expectedCommand[field](mockArgs); | ||
expect(runScriptStub.called).to.be.true; | ||
} else { | ||
expect(result[key][field]).to.deep.equal(expectedCommand[field]); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it('should return empty result when package.json contents are empty', async () => { | ||
const emptyPackage = {}; | ||
|
||
sinon.stub(resolvePkg, 'default').resolves(emptyPackage); | ||
|
||
const result = await getPackageScriptCommands(); | ||
|
||
expect(result).to.deep.equal(emptyPackage); | ||
}); | ||
|
||
it('should ignore jss script entry', async () => { | ||
const packageJson = { | ||
scripts: { | ||
jss: 'do --this', | ||
second: 'do --that', | ||
third: 'do --all', | ||
}, | ||
}; | ||
const { jss: _, ...expectedScripts } = packageJson.scripts; | ||
|
||
sinon.stub(resolvePkg, 'default').resolves(packageJson); | ||
|
||
const result = await getPackageScriptCommands(); | ||
|
||
expect(Object.keys(expectedScripts)).to.be.deep.equal(Object.keys(result)); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.