Skip to content

Commit 9ca2cd8

Browse files
authored
Route to local proxy when turbo proxy is running (#16)
1 parent b4b1101 commit 9ca2cd8

File tree

14 files changed

+36
-69
lines changed

14 files changed

+36
-69
lines changed

packages/microfrontends/src/bin/check-proxy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { localProxyIsRunning } from './local-proxy-is-running';
2+
13
export function displayLocalProxyInfo(port: number): void {
24
// TODO(olszewski): this is really icky, but since withMicroFrontends is called by two separate processes
35
// we can't rely on some shared state so we instead use env to store state
4-
const { MFE_PROXY_MESSAGE_PRINTED, TURBO_TASK_HAS_MFE_PROXY } = process.env;
56
if (
6-
TURBO_TASK_HAS_MFE_PROXY === 'true' &&
7-
MFE_PROXY_MESSAGE_PRINTED !== 'true'
7+
localProxyIsRunning() &&
8+
process.env.MFE_PROXY_MESSAGE_PRINTED !== 'true'
89
) {
910
process.env.MFE_PROXY_MESSAGE_PRINTED = 'true';
1011
// eslint-disable-next-line no-console
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function localProxyIsRunning(): boolean {
2+
return process.env.TURBO_TASK_HAS_MFE_PROXY === 'true';
3+
}

packages/microfrontends/src/bin/route-to-local-proxy.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/microfrontends/src/next/config/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ function typedEntries<T extends Record<string, unknown>>(
1212
return Object.entries(obj) as [keyof T, T[keyof T]][];
1313
}
1414

15-
function isProduction(opts?: WithMicrofrontendsOptions): boolean {
16-
if (opts?.isProduction) {
17-
return opts.isProduction();
18-
}
19-
20-
return process.env.VERCEL_ENV === 'production';
21-
}
22-
2315
/**
2416
* Automatically configures your Next.js application to work with microfrontends.
2517
*
@@ -72,7 +64,6 @@ export function withMicrofrontends(
7264
next,
7365
microfrontend: microfrontends.config,
7466
opts: {
75-
isProduction: isProduction(opts),
7667
supportPagesRouter: opts?.supportPagesRouter,
7768
},
7869
});

packages/microfrontends/src/next/config/transforms/build-id.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe('withMicrofrontends: buildId', () => {
2323
app: childApp,
2424
microfrontend: microfrontends.config,
2525
opts: {
26-
isProduction: true,
2726
supportPagesRouter: true,
2827
},
2928
});
@@ -52,7 +51,6 @@ describe('withMicrofrontends: buildId', () => {
5251
app: defaultApp,
5352
microfrontend: microfrontends.config,
5453
opts: {
55-
isProduction: true,
5654
supportPagesRouter: true,
5755
},
5856
});
@@ -69,7 +67,6 @@ describe('withMicrofrontends: buildId', () => {
6967
app: defaultApp,
7068
microfrontend: microfrontends.config,
7169
opts: {
72-
isProduction: true,
7370
supportPagesRouter: true,
7471
},
7572
}),
@@ -86,7 +83,6 @@ describe('withMicrofrontends: buildId', () => {
8683
app: childApp,
8784
microfrontend: microfrontends.config,
8885
opts: {
89-
isProduction: true,
9086
supportPagesRouter: true,
9187
},
9288
});

packages/microfrontends/src/next/config/transforms/redirects.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('withMicrofrontends: redirects', () => {
1414
describe('local proxy running', () => {
1515
beforeEach(() => {
1616
process.env = { ...OLD_ENV };
17-
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
17+
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
1818
});
1919

2020
it('redirects when enabled non-prod', async () => {
@@ -30,7 +30,6 @@ describe('withMicrofrontends: redirects', () => {
3030
next: nextConfig,
3131
app,
3232
microfrontend: mfConfig,
33-
opts: { isProduction: false },
3433
});
3534

3635
expect(newConfig.redirects).toBeDefined();
@@ -61,7 +60,6 @@ describe('withMicrofrontends: redirects', () => {
6160
next: nextConfig,
6261
app,
6362
microfrontend: mfConfig,
64-
opts: { isProduction: false },
6563
});
6664

6765
expect(newConfig.redirects).toBeDefined();
@@ -101,7 +99,6 @@ describe('withMicrofrontends: redirects', () => {
10199
next: nextConfig,
102100
app,
103101
microfrontend: mfConfig,
104-
opts: { isProduction: false },
105102
});
106103

107104
expect(newConfig.redirects).toBeDefined();
@@ -125,6 +122,7 @@ describe('withMicrofrontends: redirects', () => {
125122
});
126123

127124
it('no redirects prod', () => {
125+
process.env.TURBO_TASK_HAS_MFE_PROXY = undefined;
128126
const mfConfig = MicrofrontendsServer.fromFile({
129127
filePath: join(fixtures, 'simple.jsonc'),
130128
}).config;
@@ -137,7 +135,6 @@ describe('withMicrofrontends: redirects', () => {
137135
next: nextConfig,
138136
app,
139137
microfrontend: mfConfig,
140-
opts: { isProduction: true },
141138
});
142139

143140
expect(newConfig.redirects).toBeUndefined();
@@ -163,7 +160,7 @@ describe('withMicrofrontends: redirects', () => {
163160
});
164161

165162
it('no redirects if non dev env', () => {
166-
process.env.VERCEL_ENV = 'staging';
163+
process.env.TURBO_TASK_HAS_MFE_PROXY = undefined;
167164
const mfConfig = MicrofrontendsServer.fromFile({
168165
filePath: join(fixtures, 'simple.jsonc'),
169166
}).config;
@@ -181,8 +178,8 @@ describe('withMicrofrontends: redirects', () => {
181178
expect(newConfig.redirects).toBeUndefined();
182179
});
183180

184-
it('redirects if dev env', async () => {
185-
process.env.VERCEL_ENV = 'development';
181+
it('redirects if turbo is running mfe proxy', async () => {
182+
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
186183
const mfConfig = MicrofrontendsServer.fromFile({
187184
filePath: join(fixtures, 'simple.jsonc'),
188185
}).config;
@@ -231,7 +228,6 @@ describe('withMicrofrontends: redirects', () => {
231228
next: nextConfig,
232229
app,
233230
microfrontend: mfConfig,
234-
opts: { isProduction: false },
235231
});
236232

237233
expect(newConfig.redirects).toBeUndefined();

packages/microfrontends/src/next/config/transforms/redirects.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { routeToLocalProxy } from '../../utils/route-to-local-proxy';
1+
import { localProxyIsRunning } from '../../../bin/local-proxy-is-running';
22
import type { TransformConfigInput, TransformConfigResponse } from './types';
33

44
export function transform(args: TransformConfigInput): TransformConfigResponse {
5-
const { next, microfrontend, opts } = args;
6-
const isProduction = opts?.isProduction ?? false;
5+
const { next, microfrontend } = args;
76

87
const requireLocalProxyHeader =
9-
routeToLocalProxy() &&
10-
!isProduction &&
11-
!process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;
8+
localProxyIsRunning() && !process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;
129

1310
if (requireLocalProxyHeader) {
1411
// If local proxy is running, redirect all requests without the header set by the local proxy to the local proxy.

packages/microfrontends/src/next/config/transforms/rewrites.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ describe('withMicrofrontends: rewrites', () => {
3737
next: nextConfig,
3838
app,
3939
microfrontend: mfConfig,
40-
opts: { isProduction: true },
4140
});
4241

4342
expect(newConfig.env).toEqual({
@@ -77,7 +76,6 @@ describe('withMicrofrontends: rewrites', () => {
7776
next: nextConfig,
7877
app,
7978
microfrontend: mfConfig,
80-
opts: { isProduction: true },
8179
});
8280

8381
expect(newConfig.env).toEqual({
@@ -136,7 +134,6 @@ describe('withMicrofrontends: rewrites', () => {
136134
next: nextConfig,
137135
app,
138136
microfrontend: mfConfig,
139-
opts: { isProduction: true },
140137
});
141138

142139
expect(newConfig.env).toEqual({
@@ -209,7 +206,6 @@ describe('withMicrofrontends: rewrites', () => {
209206
next: nextConfig,
210207
app,
211208
microfrontend: mfConfig,
212-
opts: { isProduction: true },
213209
});
214210

215211
expect(newConfig.env).toEqual({
@@ -257,7 +253,6 @@ describe('withMicrofrontends: rewrites', () => {
257253
next: nextConfig,
258254
app,
259255
microfrontend: mfConfig,
260-
opts: { isProduction: true },
261256
});
262257

263258
expect(newConfig.env).toEqual({
@@ -305,7 +300,6 @@ describe('withMicrofrontends: rewrites', () => {
305300
next: nextConfig,
306301
app,
307302
microfrontend: mfConfig,
308-
opts: { isProduction: true },
309303
});
310304

311305
expect(newConfig.env).toEqual({
@@ -349,7 +343,6 @@ describe('withMicrofrontends: rewrites', () => {
349343
next: nextConfig,
350344
app,
351345
microfrontend: mfConfig,
352-
opts: { isProduction: true },
353346
});
354347

355348
expect(newConfig.rewrites).toBeDefined();
@@ -375,9 +368,6 @@ describe('withMicrofrontends: rewrites', () => {
375368
next: nextConfig,
376369
app,
377370
microfrontend: mfConfig,
378-
opts: {
379-
isProduction: false,
380-
},
381371
});
382372

383373
expect(newConfig).toMatchObject({});
@@ -399,7 +389,6 @@ describe('withMicrofrontends: rewrites', () => {
399389
next: nextConfig,
400390
app,
401391
microfrontend: mfConfig,
402-
opts: { isProduction: true },
403392
});
404393

405394
expect(newConfig.rewrites).toBeDefined();

packages/microfrontends/src/next/config/transforms/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import type {
66
} from '../../../config/microfrontends-config/isomorphic/application';
77

88
export interface TransformConfigOptions {
9-
isProduction: boolean;
9+
/**
10+
* @deprecated this is a no-op, and will be removed in a future version.
11+
* It is not necessary to specify this option.
12+
*/
13+
isProduction?: boolean;
1014
supportPagesRouter?: boolean;
1115
// Prefer the legacy behavior of using webpack.EnvironmentPlugin instead of
1216
// Next.js's `defineServer` option, even when Next.js is new enough to support it.

packages/microfrontends/src/next/config/transforms/webpack.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ describe('transform function', () => {
6464
app: currentApplication,
6565
microfrontend: microfrontends.config,
6666
opts: {
67-
isProduction: true,
6867
preferWebpackEnvironmentPlugin,
6968
},
7069
});
@@ -102,7 +101,6 @@ describe('transform function', () => {
102101
app: currentApplication,
103102
microfrontend: microfrontends.config,
104103
opts: {
105-
isProduction: true,
106104
preferWebpackEnvironmentPlugin,
107105
},
108106
});
@@ -145,7 +143,6 @@ describe('transform function', () => {
145143
app: currentApplication,
146144
microfrontend: microfrontends.config,
147145
opts: {
148-
isProduction: true,
149146
preferWebpackEnvironmentPlugin,
150147
},
151148
});

0 commit comments

Comments
 (0)