mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-01 18:32:52 +02:00
createRedirectPageContent: use eta template + encode URI for meta refresh header + tests
This commit is contained in:
parent
fde4933cd5
commit
d1a162ec30
5 changed files with 79 additions and 20 deletions
|
@ -13,6 +13,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@docusaurus/types": "^2.0.0-alpha.55",
|
"@docusaurus/types": "^2.0.0-alpha.55",
|
||||||
"@docusaurus/utils": "^2.0.0-alpha.55",
|
"@docusaurus/utils": "^2.0.0-alpha.55",
|
||||||
|
"eta": "^1.1.1",
|
||||||
"globby": "^10.0.1"
|
"globby": "^10.0.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`createRedirectPageContent should encode uri special chars 1`] = `
|
||||||
|
"<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset=\\"UTF-8\\">
|
||||||
|
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/\\">
|
||||||
|
</head>
|
||||||
|
<script>
|
||||||
|
window.location.href = 'https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/';
|
||||||
|
</script>
|
||||||
|
</html>"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`createRedirectPageContent should match snapshot 1`] = `
|
||||||
|
"<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset=\\"UTF-8\\">
|
||||||
|
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/\\">
|
||||||
|
</head>
|
||||||
|
<script>
|
||||||
|
window.location.href = 'https://docusaurus.io/';
|
||||||
|
</script>
|
||||||
|
</html>"
|
||||||
|
`;
|
|
@ -0,0 +1,26 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2017-present, Facebook, Inc.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import createRedirectPageContent from '../createRedirectPageContent';
|
||||||
|
|
||||||
|
describe('createRedirectPageContent', () => {
|
||||||
|
test('should match snapshot', () => {
|
||||||
|
expect(
|
||||||
|
createRedirectPageContent({toUrl: 'https://docusaurus.io/'}),
|
||||||
|
).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should encode uri special chars', () => {
|
||||||
|
const result = createRedirectPageContent({
|
||||||
|
toUrl: 'https://docusaurus.io/gr/σελιδας/',
|
||||||
|
});
|
||||||
|
expect(result).toContain(
|
||||||
|
'https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/',
|
||||||
|
);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
|
@ -5,6 +5,9 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const eta = require('eta');
|
||||||
|
const redirectPageTemplate = require('./template/redirectPage.html.template');
|
||||||
|
|
||||||
type CreateRedirectPageOptions = {
|
type CreateRedirectPageOptions = {
|
||||||
toUrl: string;
|
toUrl: string;
|
||||||
};
|
};
|
||||||
|
@ -12,24 +15,7 @@ type CreateRedirectPageOptions = {
|
||||||
export default function createRedirectPageContent({
|
export default function createRedirectPageContent({
|
||||||
toUrl,
|
toUrl,
|
||||||
}: CreateRedirectPageOptions) {
|
}: CreateRedirectPageOptions) {
|
||||||
return `
|
return eta.render(redirectPageTemplate.trim(), {
|
||||||
<!DOCTYPE html>
|
toUrl: encodeURI(toUrl),
|
||||||
<html lang="en-US">
|
});
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta http-equiv="refresh" content="0; url=${toUrl}">
|
|
||||||
<script>
|
|
||||||
const redirectLink = '${toUrl}' + location.search + location.hash;
|
|
||||||
document.write('<link rel="canonical" href="' + redirectLink + '">');
|
|
||||||
document.write('<title>Redirecting to ' + redirectLink + '</title>');
|
|
||||||
document.write('</head>')
|
|
||||||
document.write('<body>')
|
|
||||||
document.write('If you are not redirected automatically, follow this <a href="' + redirectLink + '">link</a>.')
|
|
||||||
document.write('</body>')
|
|
||||||
setTimeout(() => {
|
|
||||||
window.location.assign(redirectLink)
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
</html>
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="refresh" content="0; url=<%= it.toUrl %>">
|
||||||
|
</head>
|
||||||
|
<script>
|
||||||
|
window.location.href = '<%= it.toUrl %>';
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
|
`;
|
Loading…
Add table
Add a link
Reference in a new issue