Skip to content

Fix: Gracefully handle syntax errors in definePage to prevent Vite server crashes #692

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 11, 2025

Problem

Syntax errors in definePage() calls would crash the Vite development server, making it impossible to continue development when there were JavaScript syntax issues in the page definition. This created a poor developer experience where a simple typo could bring down the entire dev server.

<script setup>
// This would crash the server
definePage({
  name: 'test',,  // Extra comma - syntax error
  path: '/test'
})
</script>

Solution

Enhanced error handling throughout the definePage processing pipeline to gracefully handle syntax errors and parsing failures:

Key Changes

  1. Added comprehensive try-catch blocks in definePageTransform() and extractDefinePageNameAndPath() functions to catch any parsing or processing errors

  2. Enhanced getCodeAst() function with error handling around Babel AST parsing to prevent syntax errors from propagating

  3. Changed error behavior from throwing SyntaxErrors to logging warnings and returning safe fallback values:

    • When extracting definePage: returns 'export default {}'
    • When removing definePage from source: returns undefined (no transform)
  4. Improved developer feedback by replacing crashes with helpful warning messages that identify the problematic file

Testing

Added comprehensive test coverage for various syntax error scenarios:

  • Extra commas in object literals
  • Missing commas between properties
  • Completely malformed JavaScript
  • Unclosed brackets
  • Invalid scope references

Impact

  • ✅ Vite server no longer crashes on definePage syntax errors
  • ✅ Development can continue with broken definePage calls (they're simply ignored)
  • ✅ Clear warning messages help developers identify and fix issues
  • ✅ Maintains backward compatibility for all valid definePage usage
  • ✅ No performance impact on normal operation

The fix ensures a more robust development experience while maintaining all existing functionality for properly formatted definePage() calls.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link

cloudflare-workers-and-pages bot commented Aug 11, 2025

Deploying unplugin-vue-router with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1923444
Status: ✅  Deploy successful!
Preview URL: https://a3d014e8.unplugin-vue-router.pages.dev
Branch Preview URL: https://copilot-fix-904c72ad-c6f9-42.unplugin-vue-router.pages.dev

View logs

Co-authored-by: posva <664177+posva@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Can you fix this bug? Ideally a syntax error in definePage shouldn't crash the Vite server. I should gracefully handle it by ignoring the object (definePage) entirely Fix: Gracefully handle syntax errors in definePage to prevent Vite server crashes Aug 11, 2025
@Copilot Copilot AI requested a review from posva August 11, 2025 09:36
Copilot finished work on behalf of posva August 11, 2025 09:36
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

Successfully merging this pull request may close these issues.

2 participants