Skip to content

Instantly share code, notes, and snippets.

@ardeay
Created July 8, 2022 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardeay/bc543a7c671b5f0efe7ccd4d4422b613 to your computer and use it in GitHub Desktop.
Save ardeay/bc543a7c671b5f0efe7ccd4d4422b613 to your computer and use it in GitHub Desktop.
Redirect Example for AHS
// fetchRedirects, get the list of all redirects set in the content manager, loads into next.config.js
async function fetchZestyRedirects() {
// access the headless url map
let redirectsAPIURL = 'https://ahscom.zesty.dev/-/headless/redirects.json';
try {
const req = await fetch(redirectsAPIURL);
let redirects = await req.json();
let redirectsForNext = []
redirects.forEach(r => {
redirectsForNext.push({
source: r.path,
destination: r.target,
permanent: r.code == 301 ? true : false,
})
})
return redirectsForNext;
} catch (err){
console.log(err)
return []
}
}
module.exports = { fetchZestyRedirects };
@ardeay
Copy link
Author

ardeay commented Jul 8, 2022

Example implementation

/ next.config.js
const { fetchZestyRedirects } = require('./lib/zesty/fetchRedirects');

module.exports = {
  async redirects() {
    let myRedirects = [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ];
    const zestyRedirects = await fetchZestyRedirects();
    
    myRedirects.concat(zestyRedirects );
    return myRedirects
  },
  ...
}

@ardeay
Copy link
Author

ardeay commented Jul 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment