Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat: support redirects #21

Merged
merged 4 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions @antv/gatsby-theme-antv/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ exports.onCreateNode = ({ node, actions, getNode, store }) => {
};

exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const { createPage, createRedirect } = actions;
const result = await graphql(`
{
allMarkdownRemark(limit: 1000) {
Expand All @@ -168,6 +168,10 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
examples {
slug
}
redirects {
fromPath
toPath
}
}
}
}
Expand All @@ -177,8 +181,20 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const posts = result.data.allMarkdownRemark.edges;
const allDemos = result.data.allFile.nodes

const {
site: { siteMetadata },
allMarkdownRemark,
allFile,
} = result.data;

const { redirects } = siteMetadata;
(redirects || []).forEach(({ fromPath, toPath }) => {
createRedirect({ fromPath, toPath, isPermanent: true });
});

const posts = allMarkdownRemark.edges;
const allDemos = allFile.nodes
.filter(node => /demo\/(.*)\.[t|j]sx?$/.test(node.relativePath))
.map(item => {
const source = fs.readFileSync(item.absolutePath, 'utf8');
Expand Down Expand Up @@ -248,7 +264,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
design,
API,
};
const { siteMetadata } = result.data.site;
const examplePosts = posts
.filter(item => !!item)
.filter(
Expand Down Expand Up @@ -352,13 +367,19 @@ exports.sourceNodes = ({ actions, schema }) => {
target: String
}

type SiteSiteMetadataRedirects implements Node {
fromPath: String!
toPath: String!
}

type SiteSiteMetadata implements Node {
title: String!
description: String!
githubUrl: String!
navs: [SiteSiteMetadataNavs]
docs: [SiteSiteMetadataDocs]
examples: [SiteSiteMetadataExamples]
redirects: [SiteSiteMetadataRedirects]
showLanguageSwitcher: Boolean
playground: PlayGround
}
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ module.exports = {
playgroundDidMount: 'console.log("playgroundDidMount");',
playgroundWillUnmount: 'console.log("playgroundWillUnmount");',
},
redirects: [
{
fromPath: '/old-url',
toPath: '/new-url',
},
],
},
};
```
Expand Down
6 changes: 6 additions & 0 deletions example/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ module.exports = {
playgroundDidMount: 'console.log("playgroundDidMount");',
playgroundWillUnmount: 'console.log("playgroundWillUnmount");',
},
redirects: [
{
fromPath: '/old-url',
toPath: '/new-url',
},
],
},
};