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

Dynamic Code Evaluation prevents Flagsmith to run on Next.js middlewares #112

Closed
cagrispan opened this issue Apr 25, 2022 · 8 comments
Closed
Assignees

Comments

@cagrispan
Copy link

cagrispan commented Apr 25, 2022

Hi,

I'm trying to run flagsmith on a Next.js middleware to load different routes depending on flags as below:

import flagsmith from "flagsmith/isomorphic";
import { NextRequest, NextResponse } from "next/server";

flagsmith.init({
  environmentID: {{ENVIRONMENT_KEY}},
});

export default async function middleware(request: NextRequest) {
  const url = request.nextUrl.clone();

  if (flagsmith.hasFeature(url.pathname)) {
    const UUID = crypto.randomUUID();

    await flagsmith.identify(UUID);
    url.pathname = flagsmith.getValue(url.pathname) as string;

    const response = NextResponse.rewrite(url);

    return response;
  }
}

It runs locally, but fails on build with the error:

./node_modules/flagsmith/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/_middleware

Import trace for requested module:
./pages/_middleware.ts

I would like to know if there is a plan to support Next.js Middlewares in the near future.

For reference:

Edge Runtime Unsupported APIs


UPDATE

The Function calls are added when AsyncStorage is used. If you don't pass it as a parameter and build the project, it will not include any dynamic code evaluation on the index files.

// index.ts - line 6
const flagsmith = core({ fetch });

This is not a solution, just some progress on the investigation.

@dabeeeenster dabeeeenster added the help wanted Extra attention is needed label Apr 25, 2022
@alizain
Copy link

alizain commented Apr 26, 2022

@dabeeeenster all of the flagsmith code is obfuscated and compiled. you don't provide unobfuscated source. you've added the help-wanted label, but what's the best way for us to help?

@dabeeeenster
Copy link
Contributor

Hi @alizain - i don't follow - the source is in this repo?

@alizain
Copy link

alizain commented Apr 26, 2022

my bad, i couldn't find the correct file :)

@kyle-ssg
Copy link
Member

kyle-ssg commented Jun 8, 2022

Hey @cagrispan, this wasn't on my radar but I'd like to help with this - am a massive fan of Next.js. It might actually be worth trying with the latest version >3, calls to async storage now check for typeof window, e.g https://github.com/Flagsmith/flagsmith-js-client/blob/main/flagsmith-core.ts#L270

@kyle-ssg
Copy link
Member

kyle-ssg commented Jul 1, 2022

This is now available in flagsmith 3.3.2, there's a working example here

@kyle-ssg kyle-ssg closed this as completed Jul 1, 2022
@kyle-ssg kyle-ssg removed the help wanted Extra attention is needed label Jul 1, 2022
@cagrispan
Copy link
Author

Nice @kyle-ssg! Thank you!

@kyle-ssg
Copy link
Member

kyle-ssg commented Jul 1, 2022

No problem! I'd love to hear about what you do with it, had a play around with it today, It looks like the main use cases would be redirecting for abn tests maybe

@cagrispan
Copy link
Author

Precisely. Since it was not supported then, we are calling Flagsmith API to retrieve the flags and route based on that. And you are right, it's for ab tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants