Fix TypeError: Invalid URL when serving cards from prefix-mapped realms#4241
Merged
Fix TypeError: Invalid URL when serving cards from prefix-mapped realms#4241
Conversation
After the import maps change (55a0f23), card IDs in the index are stored in registered prefix form (e.g. @cardstack/openrouter/...) via unresolveResourceInstanceURLs. The relativizeResource function used resource.id directly as a URL base for resolveCardReference, but prefix strings are not valid URL bases for new URL(). This caused all cards in prefix-mapped realms (like the openrouter realm) to return 500 errors. Use cardIdToURL() to resolve the prefix to a real URL first, consistent with all other call sites updated in the import maps commit. Fixes CS-10498 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Tests that resolving a relative URL against a prefix-form card ID (e.g. @cardstack/openrouter/...) requires cardIdToURL() to convert the prefix to a real URL first. Without this, new URL() throws TypeError: Invalid URL because prefix strings are not valid URL bases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Export relativizeDocument and test it with a prefix-form resource ID. The test now fails without the fix (TypeError: Invalid URL) and passes with it, confirming the regression is properly guarded. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes TypeError: Invalid URL when serving cards from prefix-mapped realms by ensuring prefix-form resource.id values are resolved to real URLs before being used as the base for relative URL/module resolution.
Changes:
- Resolve
resource.idviacardIdToURL()insiderelativizeResource()to avoid passing non-URL prefix strings intonew URL(...). - Export
relativizeDocument()for regression testing. - Add shared runtime-common regression tests and wire them into the realm-server test suite.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/runtime-common/realm-index-query-engine.ts | Fixes URL base handling for prefix-form IDs during document/resource relativization. |
| packages/runtime-common/tests/card-reference-resolver-test.ts | Adds regression coverage for prefix-form data.id not causing Invalid URL. |
| packages/realm-server/tests/card-reference-resolver-test.ts | Runs the shared runtime-common regression tests under realm-server’s QUnit suite. |
| packages/realm-server/tests/index.ts | Registers the new realm-server test module in the test index. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
burieberry
approved these changes
Mar 24, 2026
The test only runs in realm-server, so there's no need for the SharedTests indirection through runtime-common. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…eMessage The test was missing await settled() after simulateRemoteMessage(), causing a race where waitFor ran before Ember processed the new message event. Also tightened the waitFor selector to wait for the actual command-apply button rather than just the message container. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…/cardstack/boxel into cs-10498-system-card-is-not-working
Preview deployments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
TypeError: Invalid URLthat broke all cards in prefix-mapped realms, causing the system card to fail on stagingAfter the import maps change (55a0f23, "Change to use import maps for @cardstack/catalog"),
unresolveResourceInstanceURLsconverts card IDs in the index from their full HTTP URLs to registered prefix form. These prefix mappings are configured in the realm server startup scripts (e.g.packages/realm-server/scripts/start-staging.sh):So a card with URL
https://realms-staging.stack.cards/openrouter/OpenRouterModel/anthropic-claude-sonnet-4-6gets its ID stored in the index as@cardstack/openrouter/OpenRouterModel/anthropic-claude-sonnet-4-6.The
relativizeResourcefunction usedresource.iddirectly as a URL base forresolveCardReference. Whenresource.idis a prefix string like@cardstack/openrouter/...,new URL('../openrouter-model', '@cardstack/openrouter/...')throwsTypeError: Invalid URLbecause prefix strings are not valid URL bases.The fix uses
cardIdToURL()to resolve the prefix to a real URL first, consistent with all other call sites that were already updated in the import maps commit.Test plan
relativizeDocumentdirectly with a prefix-form resource ID — fails without the fix (TypeError: Invalid URL), passes with itcurl -H "Accept: application/vnd.card+json" https://realms-staging.stack.cards/openrouter/OpenRouterModel/anthropic-claude-sonnet-4-6)Fixes CS-10498
🤖 Generated with Claude Code