Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/microfrontends/src/bin/check-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { localProxyIsRunning } from './local-proxy-is-running';

export function displayLocalProxyInfo(port: number): void {
// TODO(olszewski): this is really icky, but since withMicroFrontends is called by two separate processes
// we can't rely on some shared state so we instead use env to store state
const { MFE_PROXY_MESSAGE_PRINTED, TURBO_TASK_HAS_MFE_PROXY } = process.env;
if (
TURBO_TASK_HAS_MFE_PROXY === 'true' &&
MFE_PROXY_MESSAGE_PRINTED !== 'true'
localProxyIsRunning() &&
process.env.MFE_PROXY_MESSAGE_PRINTED !== 'true'
) {
process.env.MFE_PROXY_MESSAGE_PRINTED = 'true';
// eslint-disable-next-line no-console
Expand Down
3 changes: 3 additions & 0 deletions packages/microfrontends/src/bin/local-proxy-is-running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function localProxyIsRunning(): boolean {
return process.env.TURBO_TASK_HAS_MFE_PROXY === 'true';
}
5 changes: 0 additions & 5 deletions packages/microfrontends/src/bin/route-to-local-proxy.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/microfrontends/src/next/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ function typedEntries<T extends Record<string, unknown>>(
return Object.entries(obj) as [keyof T, T[keyof T]][];
}

function isProduction(opts?: WithMicrofrontendsOptions): boolean {
if (opts?.isProduction) {
return opts.isProduction();
}

return process.env.VERCEL_ENV === 'production';
}

/**
* Automatically configures your Next.js application to work with microfrontends.
*
Expand Down Expand Up @@ -72,7 +64,6 @@ export function withMicrofrontends(
next,
microfrontend: microfrontends.config,
opts: {
isProduction: isProduction(opts),
supportPagesRouter: opts?.supportPagesRouter,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('withMicrofrontends: buildId', () => {
app: childApp,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
supportPagesRouter: true,
},
});
Expand Down Expand Up @@ -52,7 +51,6 @@ describe('withMicrofrontends: buildId', () => {
app: defaultApp,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
supportPagesRouter: true,
},
});
Expand All @@ -69,7 +67,6 @@ describe('withMicrofrontends: buildId', () => {
app: defaultApp,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
supportPagesRouter: true,
},
}),
Expand All @@ -86,7 +83,6 @@ describe('withMicrofrontends: buildId', () => {
app: childApp,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
supportPagesRouter: true,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('withMicrofrontends: redirects', () => {
describe('local proxy running', () => {
beforeEach(() => {
process.env = { ...OLD_ENV };
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
});

it('redirects when enabled non-prod', async () => {
Expand All @@ -30,7 +30,6 @@ describe('withMicrofrontends: redirects', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: false },
});

expect(newConfig.redirects).toBeDefined();
Expand Down Expand Up @@ -61,7 +60,6 @@ describe('withMicrofrontends: redirects', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: false },
});

expect(newConfig.redirects).toBeDefined();
Expand Down Expand Up @@ -101,7 +99,6 @@ describe('withMicrofrontends: redirects', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: false },
});

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

it('no redirects prod', () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = undefined;
const mfConfig = MicrofrontendsServer.fromFile({
filePath: join(fixtures, 'simple.jsonc'),
}).config;
Expand All @@ -137,7 +135,6 @@ describe('withMicrofrontends: redirects', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

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

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

it('redirects if dev env', async () => {
process.env.VERCEL_ENV = 'development';
it('redirects if turbo is running mfe proxy', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
const mfConfig = MicrofrontendsServer.fromFile({
filePath: join(fixtures, 'simple.jsonc'),
}).config;
Expand Down Expand Up @@ -231,7 +228,6 @@ describe('withMicrofrontends: redirects', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: false },
});

expect(newConfig.redirects).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { routeToLocalProxy } from '../../utils/route-to-local-proxy';
import { localProxyIsRunning } from '../../../bin/local-proxy-is-running';
import type { TransformConfigInput, TransformConfigResponse } from './types';

export function transform(args: TransformConfigInput): TransformConfigResponse {
const { next, microfrontend, opts } = args;
const isProduction = opts?.isProduction ?? false;
const { next, microfrontend } = args;

const requireLocalProxyHeader =
routeToLocalProxy() &&
!isProduction &&
!process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;
localProxyIsRunning() && !process.env.MFE_DISABLE_LOCAL_PROXY_REWRITE;

if (requireLocalProxyHeader) {
// If local proxy is running, redirect all requests without the header set by the local proxy to the local proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -77,7 +76,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -136,7 +134,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -209,7 +206,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -257,7 +253,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -305,7 +300,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.env).toEqual({
Expand Down Expand Up @@ -349,7 +343,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.rewrites).toBeDefined();
Expand All @@ -375,9 +368,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: {
isProduction: false,
},
});

expect(newConfig).toMatchObject({});
Expand All @@ -399,7 +389,6 @@ describe('withMicrofrontends: rewrites', () => {
next: nextConfig,
app,
microfrontend: mfConfig,
opts: { isProduction: true },
});

expect(newConfig.rewrites).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import type {
} from '../../../config/microfrontends-config/isomorphic/application';

export interface TransformConfigOptions {
isProduction: boolean;
/**
* @deprecated this is a no-op, and will be removed in a future version.
* It is not necessary to specify this option.
*/
isProduction?: boolean;
supportPagesRouter?: boolean;
// Prefer the legacy behavior of using webpack.EnvironmentPlugin instead of
// Next.js's `defineServer` option, even when Next.js is new enough to support it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('transform function', () => {
app: currentApplication,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
preferWebpackEnvironmentPlugin,
},
});
Expand Down Expand Up @@ -102,7 +101,6 @@ describe('transform function', () => {
app: currentApplication,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
preferWebpackEnvironmentPlugin,
},
});
Expand Down Expand Up @@ -145,7 +143,6 @@ describe('transform function', () => {
app: currentApplication,
microfrontend: microfrontends.config,
opts: {
isProduction: true,
preferWebpackEnvironmentPlugin,
},
});
Expand Down
6 changes: 5 additions & 1 deletion packages/microfrontends/src/next/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export interface WithMicrofrontendsOptions {
* if it differs from the project name.
*/
appName?: string;
isProduction?: () => boolean;
/**
* @deprecated this is a no-op, and will be removed in a future version.
* It is not necessary to specify this option.
*/
isProduction?: boolean;
debug?: boolean;
skipTransforms?: TransformKeys[];
/**
Expand Down
15 changes: 7 additions & 8 deletions packages/microfrontends/src/next/middleware/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('runMicrofrontendsMiddleware', () => {
});

it('rewrites requests to local proxy if running', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand All @@ -138,7 +138,7 @@ describe('runMicrofrontendsMiddleware', () => {
});

it('does not rewrites to local proxy if disabled', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand All @@ -153,9 +153,8 @@ describe('runMicrofrontendsMiddleware', () => {
expect(response).toBeUndefined();
});

it('does not use flag value if in production', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.VERCEL_ENV = 'production';
it('does not use flag value if the local proxy is not running', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = undefined;
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand All @@ -175,7 +174,7 @@ describe('runMicrofrontendsMiddleware', () => {
});

it('does not execute flag if flag value header present', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand All @@ -195,7 +194,7 @@ describe('runMicrofrontendsMiddleware', () => {
});

it('does rewrites if flag value header true', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand All @@ -219,7 +218,7 @@ describe('runMicrofrontendsMiddleware', () => {
});

it('does not rewrites if flag value header true and mismatched path', async () => {
process.env.TURBO_TASK_HAS_MFE_PROXY = '1';
process.env.TURBO_TASK_HAS_MFE_PROXY = 'true';
modifyRouting(config.applications.docs, {
group: 'flagged',
paths: ['/docs/flagged'],
Expand Down
8 changes: 4 additions & 4 deletions packages/microfrontends/src/next/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { pathToRegexp } from 'path-to-regexp';
import type { ChildApplication } from '../../config/microfrontends-config/isomorphic/application';
import { getWellKnownClientData } from '../../config/well-known/endpoints';
import type { WellKnownClientData } from '../../config/well-known/types';
import { routeToLocalProxy } from '../utils/route-to-local-proxy';
import { localProxyIsRunning } from '../../bin/local-proxy-is-running';
import { MicrofrontendConfigIsomorphic } from '../../config/microfrontends-config/isomorphic';
import type {
MicrofrontendsMiddleware,
Expand Down Expand Up @@ -43,9 +43,9 @@ function getFlagHandler({
return async (req: NextRequest): Promise<NextResponse | undefined> => {
try {
const pathname = req.nextUrl.pathname;

const localProxyRunning = localProxyIsRunning();
// If the pattern doesn't match, we don't need to execute the flag
const flagValueFromHeader = routeToLocalProxy()
const flagValueFromHeader = localProxyRunning
? getMfeFlagHeader(req)
: null;
if (pattern.test(pathname) && (flagValueFromHeader ?? (await flagFn()))) {
Expand All @@ -63,7 +63,7 @@ function getFlagHandler({
headers,
},
};
if (routeToLocalProxy()) {
if (localProxyRunning) {
if (process.env.MFE_DEBUG) {
// eslint-disable-next-line no-console
console.log(
Expand Down

This file was deleted.