Skip to content

Commit

Permalink
fix(vue-renderer): add vary header for ‍user-agent‍ in modern serve…
Browse files Browse the repository at this point in the history
…r mode (#5807)
  • Loading branch information
Pooya Parsa committed May 25, 2019
1 parent a0958f0 commit 7e50fe7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/vue-renderer/src/renderers/modern.js
Expand Up @@ -109,4 +109,12 @@ export default class ModernRenderer extends SSRRenderer {
return linkTag.replace('rel="preload"', `rel="modulepreload"${cors}`).replace(legacyJsFile, modernJsFile)
})
}

async render(renderContext) {
const result = await super.render(renderContext)
if (this.isServerMode) {
renderContext.res.setHeader('Vary', 'User-Agent')
}
return result
}
}
8 changes: 8 additions & 0 deletions test/unit/modern.server.test.js
Expand Up @@ -68,6 +68,14 @@ describe('modern server mode', () => {
].join(', '))
})

test('Vary header should contain User-Agent', async () => {
const { headers: { vary } } = await rp(url('/'), {
resolveWithFullResponse: true,
headers: { 'user-agent': modernUA }
})
expect(vary).toContain('User-Agent')
})

// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
Expand Down

1 comment on commit 7e50fe7

@sullivanpt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the behavior I'm seeing is a bug in the auth-module or specific to this change. But ever since I upgraded to include this change I see "Can't set headers after they are sent" errors in the console when the auth-module redirects. I wonder if you need to check result before setting the header? Here is a log excerpt where I've monkey patched setHeader to show Vary being called.

i Modern bundles are detected. Modern mode (server) is enabled now.   16:08:27
SETHEADER Set-Cookie auth.strategy=google; Path=/                         16:08:46
SETHEADER Location /me/login                                              16:08:46
SETHEADER Vary User-Agent                                                 16:08:46

 ERROR  Can't set headers after they are sent.                        16:08:46

  at validateHeader (_http_outgoing.js:491:11)
  at ServerResponse.setHeader (_http_outgoing.js:498:3)
  at ServerResponse.res.setHeader (server-middleware\cookie-fixer.js:14:8)
  at ModernRenderer.render (node_modules\@nuxt\vue-renderer\dist\vue-renderer.js
:2679:25)
  at <anonymous>

Please sign in to comment.