Vue.js server-side rendering compiler that transforms Vue templates into optimized render functions for server-side execution
—
SSR-specific error handling system with dedicated error codes and messages for compilation issues unique to server-side rendering contexts.
Enumeration of error codes specific to SSR compilation issues that extend the base compiler error codes.
enum SSRErrorCodes {
/** Unsafe attribute name detected during SSR compilation */
X_SSR_UNSAFE_ATTR_NAME = 65,
/** Teleport component missing required 'to' prop */
X_SSR_NO_TELEPORT_TARGET,
/** Invalid AST node encountered during SSR transformation */
X_SSR_INVALID_AST_NODE,
}Extended compiler error interface that includes SSR-specific error codes.
interface SSRCompilerError extends CompilerError {
/** SSR-specific error code */
code: SSRErrorCodes;
}Factory function for creating SSR-specific compiler errors with appropriate error codes and location information.
/**
* Creates SSR-specific compiler errors with appropriate error codes
* @param code - SSR error code indicating the type of error
* @param loc - Optional source location where the error occurred
* @returns SSRCompilerError instance with SSR-specific error information
*/
function createSSRCompilerError(
code: SSRErrorCodes,
loc?: SourceLocation
): SSRCompilerError;Human-readable error messages corresponding to each SSR error code.
/**
* Mapping of SSR error codes to human-readable error messages
* Used for displaying meaningful error information to developers
*/
const SSRErrorMessages: { [code: number]: string };The SSRErrorMessages object contains the following mappings:
X_SSR_UNSAFE_ATTR_NAME → "Unsafe attribute name for SSR."X_SSR_NO_TELEPORT_TARGET → "Missing the 'to' prop on teleport element."X_SSR_INVALID_AST_NODE → "Invalid AST node during SSR transform."X_SSR_UNSAFE_ATTR_NAME: Indicates an attribute name that is unsafe for server-side rendering.
Common causes:
// Example usage in transform code
if (isUnsafeAttrName(attrName)) {
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME,
attr.loc
));
}X_SSR_NO_TELEPORT_TARGET: Indicates a teleport component is missing its required 'to' prop.
Common causes:
<teleport> element without a to attributeto prop that cannot be resolved at compile time// Example usage in teleport transform
if (!hasToProp(teleportNode)) {
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET,
teleportNode.loc
));
}X_SSR_INVALID_AST_NODE: Indicates an invalid or unexpected AST node during SSR transformation.
Common causes:
// Example usage in transform processing
if (!isSupportedNodeType(node)) {
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_INVALID_AST_NODE,
node.loc
));
}Always provide source location information when creating errors to help developers identify the exact location of issues in their templates:
// Good: Include location information
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME,
attr.loc
));
// Less helpful: No location information
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME
));The SSR compiler attempts to recover from errors when possible, allowing compilation to continue and report multiple issues:
// Transform continues after error reporting
try {
processUnsafeAttribute(attr, context);
} catch (error) {
context.onError(createSSRCompilerError(
SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME,
attr.loc
));
// Continue processing other attributes
}Developers can provide custom error handlers through compiler options:
import { compile } from "@vue/compiler-ssr";
const result = compile(template, {
onError: (error) => {
if (error.code === SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME) {
// Custom handling for unsafe attribute names
console.warn(`Unsafe attribute detected: ${error.message}`);
} else {
// Default error handling
throw error;
}
}
});The SSR error codes extend the base DOMErrorCodes from @vue/compiler-dom starting at the extension point (65). This ensures no conflicts with existing error codes while maintaining consistency with the broader Vue compiler error system.
// Error code numbering starts after DOMErrorCodes extension point
if (__TEST__) {
if (SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME < DOMErrorCodes.__EXTEND_POINT__) {
throw new Error(
`SSRErrorCodes need to be updated to match extension point from core DOMErrorCodes.`
);
}
}Install with Tessl CLI
npx tessl i tessl/npm-vue--compiler-ssr