build
build copied to clipboard
Adding redirects through `netlifyConfig.redirects` in build plugin is not applying redirects to the build
Describe the bug
I created a local plugin which attempts to update the netlifyConfig.redirects
array.
The plugin itself is rather simple. I renamed some values below, but the logic remains
manifest.yaml:
name: netlify-plugin-proxy-rewrite
inputs:
- name: locales
required: false
description: List of locales to apply the redirects to (EU locales)
default: de-de,en-gb,en-pl,el-gr,es-es,fr-fr,it-it,pl-pl,ro-ro,tr-tr
- name: base_host
required: false
description: The base host name for the proxy? Do not include protocol, https is automatically added
default: cool-netlify-app-here-eu.netlify.app
- name: ignore_site_id
required: false
description: The Netlify site ID for the EU site (as we do not want to run this plugin on the EU site)
default: abc123-123
Plugin's index.js file
module.exports = {
onPreBuild: function ({ inputs, netlifyConfig, utils }) {
try {
const ignoreSiteId = inputs.ignore_site_id;
if (process.env.SITE_ID === ignoreSiteId) {
console.log("Site ID is configured to be ignored. Not running");
return;
}
const localesStr = inputs.locales || "";
const localesArr = localesStr.split(",").map((item) => item.trim()); // split string by comma and strip spaces
const baseHost = inputs.base_host;
for (const locale of localesArr) {
console.log(`Adding rewrite proxy to https://${getRewriteHostPrefix()}${baseHost}/${locale}/:splat`);
netlifyConfig.redirects.push({
from: `/${locale}/*`,
to: `https://${getRewriteHostPrefix()}${baseHost}/${locale}/:splat`,
status: 200,
force: true,
});
}
} catch (err) {
utils.build.failBuild("Error processing EU proxy redirects:", { error: String(err) });
}
},
};
const getRewriteHostPrefix = function () {
const buildContext = process.env.CONTEXT;
switch (buildContext) {
case "build-preview":
return "test--";
case "branch-deploy":
return `${process.env.BRANCH}--`;
default:
return "";
}
};
When the plugin runs, I can confirm that the build process is attempting to update that array in the log output, such as the log output generated from Netlify for modifying the property.
10:04:23 AM: Netlify configuration property "redirects" value changed to [
10:04:23 AM: {
10:04:23 AM: from: '/de-de/*',
10:04:23 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/de-de/:splat',
10:04:23 AM: status: 200,
10:04:23 AM: force: true
10:04:23 AM: },
...
However, once the build completes, there are no redirect rules actually applied, as indicated by the build information at the top of the build details view (as well as just testing out the endpoints on the hosted build).
Steps to reproduce
Create a fresh netlify site with the plugin code that I mentioned in the bug description, as well as activating the plugin in your site's netlify.toml
file
Configuration
[[plugins]]
package = "/plugins/netlify/netlify-plugin-proxy-redirects"
Deploy logs
Relevant log snippet (actual site name replaced with a fake site name)
10:04:23 AM: Netlify configuration property "redirects" value changed to [
10:04:23 AM: {
10:04:23 AM: from: '/de-de/*',
10:04:23 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/de-de/:splat',
10:04:23 AM: status: 200,
10:04:23 AM: force: true
10:04:23 AM: },
10:04:23 AM: {
10:04:23 AM: from: '/en-gb/*',
10:04:23 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/en-gb/:splat',
10:04:23 AM: status: 200,
10:04:23 AM: force: true
10:04:23 AM: },
10:04:23 AM: {
10:04:23 AM: from: '/en-pl/*',
10:04:23 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/en-pl/:splat',
10:04:23 AM: status: 200,
10:04:23 AM: force: true
10:04:23 AM: },
10:04:23 AM: {
10:04:24 AM: from: '/el-gr/*',
10:04:24 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/el-gr/:splat',
10:04:24 AM: status: 200,
10:04:24 AM: force: true
10:04:24 AM: },
10:04:24 AM: {
10:04:24 AM: from: '/es-es/*',
10:04:24 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/es-es/:splat',
10:04:24 AM: status: 200,
10:04:24 AM: force: true
10:04:24 AM: },
10:04:24 AM: {
10:04:24 AM: from: '/fr-fr/*',
10:04:24 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/fr-fr/:splat',
10:04:24 AM: status: 200,
10:04:24 AM: force: true
10:04:24 AM: },
10:04:24 AM: {
10:04:24 AM: from: '/it-it/*',
10:04:24 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/it-it/:splat',
10:04:24 AM: status: 200,
10:04:24 AM: force: true
10:04:25 AM: },
10:04:25 AM: {
10:04:25 AM: from: '/pl-pl/*',
10:04:25 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/pl-pl/:splat',
10:04:25 AM: status: 200,
10:04:25 AM: force: true
10:04:25 AM: },
10:04:25 AM: {
10:04:25 AM: from: '/ro-ro/*',
10:04:25 AM: to: 'https://cool-netlify-app-here-eu.netlify.app/ro-ro/:splat',
10:04:25 AM: status: 200,
10:04:25 AM: force: true
10:04:25 AM: },
10:04:25 AM: {
10:04:25 AM: from: '/tr-tr/*',
10:04:25 AM: to: 'https://cool-netlify-app-here-eu..netlify.app/tr-tr/:splat',
10:04:25 AM: status: 200,
10:04:25 AM: force: true
10:04:25 AM: }
10:04:25 AM: ].
10:04:25 AM:
10:04:25 AM: (/plugins/netlify/netlify-plugin-eu-proxy-redirects onBuild completed in 25ms)
There is much more information in the helpdesk ticket about this specific situation that Netlify staff may need while working on this case. This link will only work for Netlify employees.
We'd love to get an answer early next week, since this seems like either a bug on our side, or a "he's holding it slightly wrong", and in that case one of the authors of the build script can likely just say "oh do x not y". If there is more debugging needed than this, please let us know :)
Following through the reproduction steps, I modified the redirect rule to the one below
netlifyConfig.redirects.push({
from: "/",
to: "https://petsofnetlify.com",
status: 200,
force: true,
})
The deploy summary shows the the redirect was processed
And visiting https://incredible-granita-3f657d.netlify.app proxies https://petsofnetlify.com. Although static assets fail to load.
So it seems like the build plugin applies redirects to the build correctly
I'd like to figure out what the difference is here, if you don't mind. My app build doesn't show any redirect rules processed in the deploy summary.
Does your app have a netlify.toml file? My app had one, but no redirects.
My app also had no _redirects
file in the publish directory.
Yeah it has a netlify.toml, https://github.com/jobala/tmp-repo, it lacks _redirects
as well.
@passion4code have you had any success in getting this to work?
Not yet. I will need a few days before I am able to look into this again.