Skip to content

Commit

Permalink
feat: Add support for 410 Gone URL (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenaSingh07 authored Jul 15, 2024
1 parent 46560cb commit 8820a42
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quintype/framework",
"version": "7.28.3",
"version": "7.28.4-410-urls.0",
"description": "Libraries to help build Quintype Node.js apps",
"main": "index.js",
"engines": {
Expand Down
80 changes: 42 additions & 38 deletions server/redirect-url-helper.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,77 @@
const url = require("url");
const logError = require("./logger").error;
const { match, compile } = require("path-to-regexp");
const url = require('url')
const logError = require('./logger').error
const { match, compile } = require('path-to-regexp')

function isUrl(url) {
function isUrl (url) {
try {
return new URL(url);
return new URL(url)
} catch (err) {
return false;
return false
}
}

function processRedirects(req, res, next, sourceUrlArray, urls) {
const query = url.parse(req.url, true) || {};
const search = query.search || "";
function processRedirects (req, res, next, sourceUrlArray, urls) {
const query = url.parse(req.url, true) || {}
const search = query.search || ''

sourceUrlArray.some((sourceUrl) => {
sourceUrlArray.some(sourceUrl => {
try {
const statusCode = parseInt(urls[sourceUrl].statusCode, 10)
if (statusCode === 410) {
res.sendStatus(410)
return true
}
if (urls[sourceUrl]) {
const destinationPath = urls[sourceUrl].destinationUrl;
const destinationPath = urls[sourceUrl].destinationUrl
const extractedSourceUrl = match(sourceUrl, {
decode: decodeURIComponent,
});
const destinationUrl = isUrl(destinationPath);
decode: decodeURIComponent
})
const destinationUrl = isUrl(destinationPath)
if (extractedSourceUrl) {
let extractedDestinationUrl;
let extractedDestinationUrl
if (destinationUrl) {
extractedDestinationUrl = compile(destinationUrl.pathname, {
encode: encodeURIComponent,
});
encode: encodeURIComponent
})
} else {
extractedDestinationUrl = compile(destinationPath, {
encode: encodeURIComponent,
});
encode: encodeURIComponent
})
}
const dynamicKeys = extractedSourceUrl(req.path);
const compiledPath = dynamicKeys && extractedDestinationUrl(dynamicKeys.params);
const dynamicKeys = extractedSourceUrl(req.path)
const compiledPath = dynamicKeys && extractedDestinationUrl(dynamicKeys.params)
if (compiledPath) {
const validStatusCodes = { 301: "max-age=604800", 302: "max-age=86400" };
const statusCode = parseInt(urls[sourceUrl].statusCode, 10);
const cacheValue = validStatusCodes[statusCode];
const validStatusCodes = { 301: 'max-age=604800', 302: 'max-age=86400' }
const cacheValue = validStatusCodes[statusCode]
if (cacheValue) {
res.set("cache-control", `public,${cacheValue}`);
res.set('cache-control', `public,${cacheValue}`)
}
res.redirect(
statusCode,
destinationUrl
? `${destinationUrl.protocol}//${destinationUrl.hostname}${compiledPath}${search}`
: `${compiledPath}${search}`
);
return true;
)
return true
}
}
}
} catch (err) {
console.log(`Redirection error on ${req.host}-----`, err);
console.log(`Redirection error on ${req.host}-----`, err)
}
});
})
}

exports.getRedirectUrl = async function getRedirectUrl(req, res, next, { redirectUrls, config }) {
let sourceUrls;
if (typeof redirectUrls === "function") {
const redirectUrlsList = await redirectUrls(config);
sourceUrls = Object.keys(redirectUrlsList);
exports.getRedirectUrl = async function getRedirectUrl (req, res, next, { redirectUrls, config }) {
let sourceUrls
if (typeof redirectUrls === 'function') {
const redirectUrlsList = await redirectUrls(config)
sourceUrls = Object.keys(redirectUrlsList)
if (sourceUrls.length > 0) {
processRedirects(req, res, next, sourceUrls, redirectUrlsList);
processRedirects(req, res, next, sourceUrls, redirectUrlsList)
}
} else if (redirectUrls) {
sourceUrls = Object.keys(redirectUrls);
sourceUrls.length > 0 && processRedirects(req, res, next, sourceUrls, redirectUrls);
sourceUrls = Object.keys(redirectUrls)
sourceUrls.length > 0 && processRedirects(req, res, next, sourceUrls, redirectUrls)
}
};
}

0 comments on commit 8820a42

Please sign in to comment.