Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding redirects through netlifyConfig.redirects in build plugin is not applying redirects to the build #4838

Open
passion4code opened this issue Jan 26, 2023 · 7 comments
Labels
type: bug code to address defects in shipped code

Comments

@passion4code
Copy link

passion4code commented Jan 26, 2023

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)
@passion4code passion4code added the type: bug code to address defects in shipped code label Jan 26, 2023
@fool
Copy link

fool commented Jan 27, 2023

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.

@fool
Copy link

fool commented Jan 27, 2023

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 :)

@lukasholzer lukasholzer self-assigned this Jan 31, 2023
@jobala jobala assigned jobala and unassigned lukasholzer Jan 31, 2023
@jobala
Copy link
Contributor

jobala commented Feb 1, 2023

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

Screenshot 2023-02-01 at 05 15 26

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

@passion4code
Copy link
Author

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.

@jobala
Copy link
Contributor

jobala commented Feb 1, 2023

Yeah it has a netlify.toml, https://github.com/jobala/tmp-repo, it lacks _redirects as well.

@jobala
Copy link
Contributor

jobala commented Feb 1, 2023

@passion4code have you had any success in getting this to work?

@passion4code
Copy link
Author

Not yet. I will need a few days before I am able to look into this again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

4 participants