Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/redirects-list.json β†’ redirects-list.json
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

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

good catch!

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops. Maybe we should try to thoroughly test redirects manually (sanity check) even if we have tests because manually you can come up with ways to try to break the logic πŸ˜‹ This is because publishing incorrect redirects (especially 301) causes almost permanent "damage" to new users browser cache. Just a thought

Copy link
Contributor

Choose a reason for hiding this comment

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

Needless to say I'm available to do this but just need to prioritize as I've been struggling with switching from task to task recently.

"^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",
Expand Down
2 changes: 1 addition & 1 deletion scripts/link-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 41 additions & 44 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})
}
})
2 changes: 1 addition & 1 deletion src/redirects.js β†’ src/utils/redirects.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/redirects.test.js β†’ src/utils/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})
Expand Down