diff --git a/src/redirects-list.json b/redirects-list.json similarity index 89% rename from src/redirects-list.json rename to redirects-list.json index e9541d6c12..b8f64af146 100644 --- a/src/redirects-list.json +++ b/redirects-list.json @@ -1,7 +1,7 @@ [ "^https://(?:www\\.)dvc\\.org/(.+)? https://dvc.org/$1", "^https://man\\.dvc\\.org/(.+)? https://dvc.org/doc/command-reference/$1 303", - "^https://error\\.dvc\\.org/(.+)? https://dvc.org/user-guide/troubleshooting#$1 303", + "^https://error\\.dvc\\.org/(.+)? https://dvc.org/doc/user-guide/troubleshooting#$1 303", "^https://(code|data|remote)\\.dvc\\.org/(.+) https://s3-us-east-2.amazonaws.com/dvc-public/$1/$2 303", "^/((?:deb|rpm)/.+) https://s3-us-east-2.amazonaws.com/dvc-s3-repo/$1 303", "^/(?:help|chat)/?$ https://discordapp.com/invite/dvwXA2N 303", diff --git a/scripts/link-check.sh b/scripts/link-check.sh index fab8b730a8..a02a790ab3 100755 --- a/scripts/link-check.sh +++ b/scripts/link-check.sh @@ -46,7 +46,7 @@ fails=0 for file in "$@"; do echo -n "$file:" prev=$fails - checker $(finder "$file" | sort -u | comm -23 - <(echo "$exclude" | sort -u)) || fails=$(($fails + 1)) + checker $(finder "$file" | sed 's/#.*//g' | sort -u | comm -23 - <(echo "$exclude" | sort -u)) || fails=$(($fails + 1)) [ $prev -eq $fails ] && echo OK done [ $fails -eq 0 ] || echo -e "ERROR:$fails failures\n---" >&2 diff --git a/server.js b/server.js index 98f612db52..aece05494d 100644 --- a/server.js +++ b/server.js @@ -14,59 +14,56 @@ const { parse } = require('url') const next = require('next') const { getItemByPath } = require('./src/utils/sidebar') -const { getRedirect } = require('./src/redirects.js') +const { getRedirect } = require('./src/utils/redirects') const dev = process.env.NODE_ENV !== 'production' const app = next({ dev }) const handle = app.getRequestHandler() const port = process.env.PORT || 3000 -// Do not run the server if the module is being required by something else -if (module.parent == null) { - app.prepare().then(() => { - createServer((req, res) => { - const parsedUrl = parse(req.url, true) - const { pathname, query } = parsedUrl - const host = req.headers.host +app.prepare().then(() => { + createServer((req, res) => { + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl + const host = req.headers.host + /* + * HTTP redirects + */ + let [redirectCode, redirectLocation] = getRedirect(host, pathname, { + req, + dev + }) + + if (redirectLocation) { + // should be getting the query as a string + const { query } = parse(req.url) + if (query) { + redirectLocation += '?' + query + } + res.writeHead(redirectCode, { + 'Cache-control': 'no-cache', + Location: redirectLocation + }) + res.end() + } else if (/^\/doc(\/.*)?$/.test(pathname)) { /* - * HTTP redirects + * Docs Engine handler */ - let [redirectCode, redirectLocation] = getRedirect(host, pathname, { - req, - dev - }) - if (redirectLocation) { - // should be getting the query as a string - const { query } = parse(req.url) - if (query) { - redirectLocation += '?' + query - } - res.writeHead(redirectCode, { - 'Cache-control': 'no-cache', - Location: redirectLocation - }) - res.end() - } else if (/^\/doc(\/.*)?$/.test(pathname)) { - /* - * Docs Engine handler - */ - - // Force 404 response code for any inexistent /doc item. - if (!getItemByPath(pathname)) { - res.statusCode = 404 - } - - // Custom route for all docs - app.render(req, res, '/doc', query) - } else { - // Regular Next.js handler - handle(req, res, parsedUrl) + // Force 404 response code for any inexistent /doc item. + if (!getItemByPath(pathname)) { + res.statusCode = 404 } - }).listen(port, err => { - if (err) throw err - console.info(`> Ready on localhost:${port}`) - }) + + // Custom route for all docs + app.render(req, res, '/doc', query) + } else { + // Regular Next.js handler + handle(req, res, parsedUrl) + } + }).listen(port, err => { + if (err) throw err + console.info(`> Ready on localhost:${port}`) }) -} +}) diff --git a/src/redirects.js b/src/utils/redirects.js similarity index 95% rename from src/redirects.js rename to src/utils/redirects.js index f9ddfc7aa2..58b86cc110 100644 --- a/src/redirects.js +++ b/src/utils/redirects.js @@ -1,6 +1,6 @@ /* eslint-env node */ -let redirects = require('./redirects-list.json') +let redirects = require('../../redirects-list.json') const processRedirectString = redirectString => { let [regex, replace, code = 301] = redirectString.split(/\s+/g) diff --git a/src/redirects.test.js b/src/utils/redirects.test.js similarity index 97% rename from src/redirects.test.js rename to src/utils/redirects.test.js index c01d111a9e..c31596221d 100644 --- a/src/redirects.test.js +++ b/src/utils/redirects.test.js @@ -83,13 +83,13 @@ describe('getRedirect', () => { itRedirects( 'https://error.dvc.org/', - 'https://dvc.org/user-guide/troubleshooting#', + 'https://dvc.org/doc/user-guide/troubleshooting#', 303 ) itRedirects( 'https://error.dvc.org/foo', - 'https://dvc.org/user-guide/troubleshooting#foo', + 'https://dvc.org/doc/user-guide/troubleshooting#foo', 303 ) })