从CodeSandbox部署作品集至Netlify时遇未知错误求助
Let’s tackle this head-on—you’re seeing a generic "unknown error" when deploying your portfolio sandbox to Netlify, but the console reveals the real issue: a CORS policy block is stopping CodeSandbox from communicating with Netlify’s deploy endpoint (the critical Access-Control-Allow-Origin header is missing). The silver lining? You have a structurally similar sandbox that deploys perfectly, so we can use that as a reference to narrow down the problem.
Here’s what to try step by step:
1. Cross-Check Deploy Settings Against Your Working Sandbox
Pop open both your problematic portfolio sandbox and the working client sandbox side by side. Head to the Deploy tab (or Netlify integration settings) in each and verify:
- The
distdirectory is correctly set as the output folder (confirm yourbuildscript actually generates files here) - The
buildCommandis exactlybuild—no typos, extra spaces, or missing scripts in yourpackage.json - Any environment variables or deploy flags are identical between the two. Sometimes a tiny config difference is all it takes to break the integration.
2. Wipe CodeSandbox Cache & Restart the Environment
Stale cache in CodeSandbox can cause weird CORS glitches that don’t make much sense. Do this:
- Hit the Restart button in the top toolbar to refresh the sandbox
- Go to File > Clear Cache to nuke any old build artifacts or session data
- Wait for the sandbox to reload, then try deploying again. This fixes more random issues than you’d think.
3. Test the Build Manually First
Before even trying to deploy, run your build command in the CodeSandbox terminal:
npm run build
- Check if the
distfolder pops up with all your expected files (HTML, CSS, images, etc.) - If the build fails here, fix those errors first—broken builds can trigger unexpected downstream errors that look like CORS issues.
4. Bypass the CodeSandbox-Netlify Integration for a Test
If the above steps don’t work, let’s rule out the integration itself:
- Export your sandbox code as a ZIP (File > Export > Export to ZIP)
- Unzip it locally, run
npm installandnpm run buildto confirm the build works on your machine - Head to your Netlify dashboard, drag-and-drop the
distfolder right into the deploy area. If this works, the problem is with the CodeSandbox-Netlify connection, not your code.
5. Dig Into Dependency & Config Differences
Even if you think dependencies are the same, double-check:
- Compare
package.jsonfiles line by line—look for differences in devDependency versions, script commands, or lock files (package-lock.json/yarn.lock) - Check for any hidden config files like
netlify.tomlor_redirectsthat might exist in one sandbox but not the other. These can silently affect deploy behavior.
Since your other sandbox deploys without a hitch, the fix is almost certainly a small, easy-to-miss detail in your config or cache. Start with the simplest checks first, and you’ll have it deployed in no time.
内容的提问来源于stack exchange,提问作者mycoal

