Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Fix SSR Decider for Bots
Browse files Browse the repository at this point in the history
#371

Co-authored-by: Giancarlo Anemone <[email protected]>
  • Loading branch information
2 people authored and fusion-bot[bot] committed Mar 20, 2019
1 parent 4ef36ea commit a4877a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/__tests__/app.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test('context composition', async t => {
chunkIdZero.set('es5', 'es5-file.js');
chunkUrlMap.set(0, chunkIdZero);
const context = {
method: 'GET',
headers: {accept: 'text/html'},
path: '/',
syncChunks: [0],
Expand Down Expand Up @@ -66,6 +67,7 @@ test('context composition with a cdn', async t => {
chunkIdZero.set('es5', 'es5-file.js');
chunkUrlMap.set(0, chunkIdZero);
const context = {
method: 'GET',
headers: {accept: 'text/html'},
path: '/',
syncChunks: [0],
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ test('ssr with bot user agent', async t => {
// $FlowFixMe

let initialCtx = {
method: 'GET',
headers: {
accept: '*/*',
'user-agent': 'AdsBot-Google',
Expand All @@ -147,6 +148,39 @@ test('ssr with bot user agent', async t => {
t.end();
});

test('POST request with bot user agent', async t => {
const flags = {render: false};
const element = 'hi';
const render = () => {
flags.render = true;
return 'lol';
};
const app = new App(element, render);

app.middleware(async (ctx, next) => {
t.notOk(ctx.element, 'does not set ctx.element');
ctx.body = 'OK';
await next();
});
try {
let initialCtx = {
method: 'POST',
headers: {
accept: '*/*',
'user-agent': 'AdsBot-Google',
},
};
// $FlowFixMe
const ctx = await run(app, initialCtx);
t.notOk(ctx.rendered, 'does not set ctx.rendered');
t.equal(ctx.body, 'OK', 'sets ctx.body');
t.equal(flags.render, false, 'does not call render');
} catch (e) {
t.ifError(e, 'should not error');
}
t.end();
});

test('ssr without valid accept header', async t => {
const flags = {render: false};
const element = 'hi';
Expand All @@ -155,6 +189,7 @@ test('ssr without valid accept header', async t => {
};
const app = new App(element, render);
let initialCtx = {
method: 'GET',
headers: {accept: '*/*'},
};
try {
Expand All @@ -179,6 +214,7 @@ test('ssr without valid bot user agent', async t => {
};
const app = new App(element, render);
let initialCtx = {
method: 'GET',
headers: {
accept: '*/*',
'user-agent': 'test',
Expand Down Expand Up @@ -232,6 +268,7 @@ test('disable SSR by composing SSRDecider with a plugin', async t => {

try {
let initialCtx = {
method: 'GET',
path: '/foo',
};
// $FlowFixMe
Expand Down Expand Up @@ -278,6 +315,7 @@ test('disable SSR by composing SSRDecider with a function', async t => {

try {
let initialCtx = {
method: 'GET',
path: '/foo',
};
// $FlowFixMe
Expand Down Expand Up @@ -327,6 +365,7 @@ test('SSR extension handling', async t => {
for (let i in extensionToSSRSupported) {
flags.render = false;
let initialCtx = {
method: 'GET',
path: `/some-path.${i}`,
};
// $FlowFixMe
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function getContext() {
return __BROWSER__
? {}
: {
method: 'GET',
path: '/',
headers: {
accept: 'text/html',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SSRDecider = createPlugin<{}, SSRDeciderService>({
// Bots don't always include the accept header.
if (ctx.headers['user-agent']) {
const agent = ctx.headers['user-agent'];
if (botRegex.test(agent)) {
if (botRegex.test(agent) && ctx.method === 'GET') {
return true;
}
}
Expand Down

0 comments on commit a4877a4

Please sign in to comment.