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

Next.js: not able to use /api routes #29

Open
danilowoz opened this issue Mar 7, 2023 · 2 comments
Open

Next.js: not able to use /api routes #29

danilowoz opened this issue Mar 7, 2023 · 2 comments

Comments

@danilowoz
Copy link
Collaborator

danilowoz commented Mar 7, 2023

Discussed in codesandbox/sandpack#808

Originally posted by ismaelrumzan March 6, 2023
Hi,
I'm trying to set up an embed codesandbox for an API edge function code example.
It works in the sandbox here:
https://codesandbox.io/p/sandbox/hopeful-lake-4ncj08

When I try to use a similar example (JS version) in the code embed, I get failed to load in the preview. This is my code.
Any suggestions?

import {
  SandpackProvider,
  SandpackLayout,
  SandpackCodeEditor,
  SandpackFileExplorer,
  SandpackPreview,
} from "@codesandbox/sandpack-react";

const filename = '/pages/api/hello.js';

const files={};

files[filename] = {
code: `import { NextResponse } from 'next/server';
export const config = {
    runtime: 'edge',
};

export default (req) => {
    return NextResponse.json({
      name: 'Hello now an Edge Function!',
    });
  };
  `,
};

  files['/pages/index.js'] = {
    code: `import useSwr from 'swr';
    const fetcher = (url) => fetch(url).then((res) => res.json());
    export default function Index() {
    const { data, error, isLoading } = useSwr('/api/hello', fetcher)

  if (error) return <div>Failed to load</div>
  if (isLoading) return <div>Loading...</div>
  if (!data) return null

  return (
  <div>Got data</div>
  )
}
    `
  }

export default () => (
  <SandpackProvider template="nextjs" files={files} customSetup={{
          dependencies: {
            swr: 'latest',
          },
        }}>
    <SandpackLayout>
      <SandpackFileExplorer />
      <SandpackCodeEditor />
      <SandpackPreview />
    </SandpackLayout>
  </SandpackProvider>
);

Reproducible sandbox: https://codesandbox.io/s/laughing-yalow-cb3c03?file=/src/App.tsx

@danilowoz danilowoz transferred this issue from codesandbox/sandpack Mar 7, 2023
@danilowoz
Copy link
Collaborator Author

@DeMoorJasper, could you please take a look at this issue?

@DeMoorJasper
Copy link
Collaborator

Yes, will have a look today

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

2 participants