-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-canonical-urls): add option to strip query string (#…
- Loading branch information
Showing
6 changed files
with
201 additions
and
19 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
58 changes: 58 additions & 0 deletions
58
packages/gatsby-plugin-canonical-urls/src/__tests__/gatsby-browser.js
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,58 @@ | ||
const { onRouteUpdate } = require(`../gatsby-browser`) | ||
|
||
describe(`gatsby-plugin-canonical-urls`, () => { | ||
beforeEach(() => { | ||
global.document.head.innerHTML = `<link data-basehost="someurl.com" data-baseprotocol="http:" href="http://someurl.com/somepost" rel="canonical" | ||
/>` | ||
}) | ||
|
||
it(`should update the href`, () => { | ||
onRouteUpdate({ location: { pathname: `/hogwarts`, hash: ``, search: `` } }) | ||
|
||
expect(global.document.head.innerHTML).toMatchInlineSnapshot( | ||
`"<link data-basehost=\\"someurl.com\\" data-baseprotocol=\\"http:\\" href=\\"http://someurl.com/hogwarts\\" rel=\\"canonical\\">"` | ||
) | ||
}) | ||
it(`should keep the hash`, () => { | ||
onRouteUpdate({ | ||
location: { pathname: `/hogwarts`, hash: `#harry-potter`, search: `` }, | ||
}) | ||
|
||
expect(global.document.head.innerHTML).toMatchInlineSnapshot( | ||
`"<link data-basehost=\\"someurl.com\\" data-baseprotocol=\\"http:\\" href=\\"http://someurl.com/hogwarts#harry-potter\\" rel=\\"canonical\\">"` | ||
) | ||
}) | ||
it(`shouldn't strip search parameter by default`, () => { | ||
onRouteUpdate({ | ||
location: { | ||
pathname: `/hogwarts`, | ||
hash: ``, | ||
search: `?house=gryffindor`, | ||
}, | ||
}) | ||
|
||
expect(global.document.head.innerHTML).toMatchInlineSnapshot( | ||
`"<link data-basehost=\\"someurl.com\\" data-baseprotocol=\\"http:\\" href=\\"http://someurl.com/hogwarts?house=gryffindor\\" rel=\\"canonical\\">"` | ||
) | ||
}) | ||
it(`should strip search paramaters if option stripQueryString is true`, () => { | ||
const pluginOptions = { | ||
stripQueryString: true, | ||
} | ||
|
||
onRouteUpdate( | ||
{ | ||
location: { | ||
pathname: `/hogwarts`, | ||
hash: ``, | ||
search: `?house=gryffindor`, | ||
}, | ||
}, | ||
pluginOptions | ||
) | ||
|
||
expect(global.document.head.innerHTML).toMatchInlineSnapshot( | ||
`"<link data-basehost=\\"someurl.com\\" data-baseprotocol=\\"http:\\" href=\\"http://someurl.com/hogwarts\\" rel=\\"canonical\\">"` | ||
) | ||
}) | ||
}) |
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
28 changes: 18 additions & 10 deletions
28
packages/gatsby-plugin-canonical-urls/src/gatsby-browser.js
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 |
---|---|---|
@@ -1,14 +1,22 @@ | ||
exports.onRouteUpdate = ({ location }) => { | ||
exports.onRouteUpdate = ( | ||
{ location }, | ||
pluginOptions = { stripQueryString: false } | ||
) => { | ||
const domElem = document.querySelector(`link[rel='canonical']`) | ||
var existingValue = domElem.getAttribute(`href`) | ||
var baseProtocol = domElem.getAttribute(`data-baseProtocol`) | ||
var baseHost = domElem.getAttribute(`data-baseHost`) | ||
const existingValue = domElem.getAttribute(`href`) | ||
const baseProtocol = domElem.getAttribute(`data-baseProtocol`) | ||
const baseHost = domElem.getAttribute(`data-baseHost`) | ||
if (existingValue && baseProtocol && baseHost) { | ||
domElem.setAttribute( | ||
`href`, | ||
`${baseProtocol}//${baseHost}${location.pathname}${location.search}${ | ||
location.hash | ||
}` | ||
) | ||
let value = `${baseProtocol}//${baseHost}${location.pathname}` | ||
|
||
const { stripQueryString } = pluginOptions | ||
|
||
if (!stripQueryString) { | ||
value += location.search | ||
} | ||
|
||
value += location.hash | ||
|
||
domElem.setAttribute(`href`, `${value}`) | ||
} | ||
} |
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