From d399b3b263e7c0ec0757748a9be0845660587c3a Mon Sep 17 00:00:00 2001 From: Norbert Sztyk Date: Thu, 23 Nov 2023 14:21:03 +0100 Subject: [PATCH] Add preview version for _experimentalPrependParentSlug --- package.json | 2 +- src/gatsby-node.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5d71d53..c0ade85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mediporta/gatsby-plugin-nginx-redirect", - "version": "0.0.14", + "version": "0.0.14-preview.1", "description": "Adds your redirects to nginx conf on gatsby build", "main": "index.js", "scripts": { diff --git a/src/gatsby-node.js b/src/gatsby-node.js index 2212401..89ca6ba 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -51,7 +51,7 @@ const recursiveSearch = (needle, object) => { export async function onPostBuild( { store, reporter }, - { outputConfigFile, inputConfigFile, whereToIncludeRedirects = "server" } + { outputConfigFile, inputConfigFile, whereToIncludeRedirects = "server", _experimentalPrependParentSlug = false } ) { const { redirects } = store.getState(); removeSync(outputConfigFile); @@ -67,9 +67,20 @@ export async function onPostBuild( conf.flush(); await sleep(500); + const nodes = getNodes() + var fields = nodes + .map(k => k.fields) + .filter(k => k !== undefined) + let foundObject = searchObject(whereToIncludeRedirects, conf.nginx); if (foundObject) { redirects.forEach((redirect) => { + + if (_experimentalPrependParentSlug) { + var field = fields.find(f => f?.slug === redirect.toPath) + redirect.toPath = field.parentSlug + "/" + redirect.toPath; + } + foundObject._add( 'rewrite', `^${redirect.fromPath}\\/?$ ${redirect.toPath} ${redirect.isPermanent ? "permanent" : "redirect"}`