Skip to content

Commit

Permalink
fix: use SLB-Forwarded headers when proxying webforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Leksat committed Feb 17, 2025
1 parent ba76230 commit e350106
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
19 changes: 14 additions & 5 deletions apps/cms/gatsby-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ export const createPages = async ({ actions }) => {
});

// Proxy Drupal webforms.
const netlifyUrl = new URL(
process.env.NETLIFY_URL || 'http://127.0.0.1:8000',
);
Object.values(Locale).forEach((locale) => {
actions.createRedirect({
fromPath: `/${locale}/form/*`,
toPath: `${process.env.GATSBY_DRUPAL_URL}/${locale}/form/:splat`,
statusCode: 200,
});
global.netlifyTomlParts.push(`
[[redirects]]
from = "/${locale}/form/*"
to = "${process.env.GATSBY_DRUPAL_URL}/${locale}/form/:splat"
status = 200
force = true
[redirects.headers]
SLB-Forwarded-Proto = "${netlifyUrl.protocol.slice(0, -1)}"
SLB-Forwarded-Host = "${netlifyUrl.host}"
SLB-Forwarded-Port = "${netlifyUrl.port}"
`);
});

// Additionally proxy themes and modules as they can have additional
Expand Down
3 changes: 3 additions & 0 deletions apps/website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ persisted-store

# Deno comes with "netlify dev". We don't need its lock file.
/deno.lock

# Created by gatsby-node.mjs
netlify.toml
59 changes: 54 additions & 5 deletions apps/website/gatsby-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import {
Locale,
NotFoundPageQuery,
} from '@custom/schema';
import fs from 'fs';
import { resolve } from 'path';

if (!('netlifyTomlParts' in global)) {
Object.defineProperty(global, 'netlifyTomlParts', {
value: [],
writable: true,
});
}

/**
* @type {import('gatsby').GatsbyNode['onCreateWebpackConfig']}
*/
Expand Down Expand Up @@ -109,9 +117,50 @@ export const createPages = async ({ actions }) => {
// Any unhandled requests are handed to strangler, which will try to pass
// them to all registered legacy systems and return 404 if none of them
// respond.
actions.createRedirect({
fromPath: '/*',
toPath: `/.netlify/functions/strangler`,
statusCode: 200,
});
// We put it into netlify.toml because it should be really the last one.
// See https://docs.netlify.com/routing/redirects/#rule-processing-order
// @ts-expect-error Not typed.
global.netlifyTomlParts.push(`
[[redirects]]
from = "/*"
to = "/.netlify/functions/strangler"
status = 200
`);
};

/**
*
* @type {import('gatsby').GatsbyNode['onPostBuild']}
*/
export const onPostBuild = async () => {
const netlifyTomlBase = fs
.readFileSync(resolve('./netlify-base.toml'), 'utf8')
.trim();
/** @type {Array<string>} */
const netlifyTomlParts =
// @ts-expect-error Not typed.
global.netlifyTomlParts;
fs.writeFileSync(
resolve('./netlify.toml'),
[netlifyTomlBase]
.concat(netlifyTomlParts.map(removeIndentation))
.join('\n\n'),
);
};

/**
* @param {string} str
* @returns {string}
*/
function removeIndentation(str) {
const lines = str.split('\n');
const minIndent = Math.min(
...lines
.filter((line) => line.trim().length > 0)
.map((line) => (line.match(/^\s*/) || [''])[0].length),
);
return lines
.map((line) => line.slice(minIndent))
.join('\n')
.trim();
}
File renamed without changes.

0 comments on commit e350106

Please sign in to comment.