Skip to content

Conversation

@saseungmin
Copy link
Member

@saseungmin saseungmin commented Aug 25, 2025

  • add trailing-slash handling for WebView baseUrl in inline mode
  • propagate origin/playerVars into local HTML
  • refine YoutubeView WebView source resolution

Summary by CodeRabbit

  • New Features

    • None.
  • Bug Fixes

    • Normalize inline WebView base URL with trailing slash and safe default.
    • Align inline baseUrl with IFrame origin; propagate origin and playerVars into local HTML.
    • Refine WebView source resolution.
    • Improve error logging for WebView messages.
  • Documentation

    • Clarify webViewUrl/inline behavior: exact origin match, include port when applicable, use trailing slash on baseUrl.
  • Chores

    • Add patch release entries for react-native-youtube-bridge.

- add trailing-slash handling for WebView baseUrl in inline mode
- propagate origin/playerVars into local HTML
- refine YoutubeView WebView source resolution
@changeset-bot
Copy link

changeset-bot bot commented Aug 25, 2025

🦋 Changeset detected

Latest commit: 279d5bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-native-youtube-bridge Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link

coderabbitai bot commented Aug 25, 2025

Walkthrough

Adds changesets for a patch release. Updates YoutubeView inline WebView base URL normalization, aligns iframe origin propagation (defaulting to https://localhost), refines WebView source resolution, and enhances error logging. Updates TSDoc for webViewUrl/inline behavior; no exported/public signatures changed.

Changes

Cohort / File(s) Summary
Release notes (changesets)
.changeset/fresh-drinks-slide.md, .changeset/slow-kings-lay.md
Add patch release entries documenting fixes for inline baseUrl/origin alignment and TSDoc updates; no code changes here.
YoutubeView inline baseUrl & logging
packages/react-native-youtube-bridge/src/YoutubeView.tsx
Normalize WebView baseUrl to ensure trailing slash with safe default (https://localhost/) for inline mode; refine source resolution; add error logging including raw event data in handleMessage.
Local HTML origin propagation
packages/react-native-youtube-bridge/src/hooks/useCreateLocalPlayerHtml.ts
Ensure origin is always set, defaulting to https://localhost when falsy; affects iframe init/playerVars.
TSDoc for webViewUrl/baseUrl
packages/react-native-youtube-bridge/src/types/youtube.ts
Tighten docs: origin must exactly match (including port), baseUrl should have trailing slash; no type/signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as App
  participant YV as YoutubeView
  participant WV as WebView (inline)
  participant IF as YouTube Iframe

  Note over YV: Compute baseUrl -> ensure trailing slash<br/>Default to https://localhost/ if missing
  App->>YV: render({ webViewBaseUrl, origin, playerVars, useInlineHtml })
  YV->>WV: source(html, baseUrl = normalizedBaseUrl)
  YV->>WV: inject playerVars including origin (default https://localhost)

  WV->>IF: load local HTML (uses playerVars.origin)
  IF-->>WV: postMessage(player events)
  WV-->>YV: onMessage(event)
  Note over YV: handleMessage with improved error logging<br/>(logs error + raw event data)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

I nudge my whiskers at the screen so bright,
A trailing slash now clicks in place just right.
Origins aligned, no ghostly ports to fear,
The iframe hums—localhost standing near.
Hop, hop! Logs caught a message in mid-flight,
Patch perfect, carrot-coded, tight. 🥕✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/inline-origin-baseurl

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@cloudflare-workers-and-pages
Copy link

Deploying react-native-youtube-bridge-example with  Cloudflare Pages  Cloudflare Pages

Latest commit: 279d5bf
Status: ✅  Deploy successful!
Preview URL: https://0be86008.react-native-youtube-bridge-example.pages.dev
Branch Preview URL: https://fix-inline-origin-baseurl.react-native-youtube-bridge-example.pages.dev

View logs

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
.changeset/fresh-drinks-slide.md (1)

5-9: Changelog reads well; consider adding a concrete example to prevent origin/baseUrl confusion.

Suggestion:

  • Show a one-liner example pairing: baseUrl https://localhost:8081/ ⇄ origin https://localhost:8081.
  • Call out that if a custom baseUrl is used, origin must also be provided (or will fall back to https://localhost).
 fix: align inline baseUrl with IFrame origin

 - add trailing-slash handling for WebView baseUrl in inline mode
 - propagate origin/playerVars into local HTML
 - refine YoutubeView WebView source resolution
+ 
+ Example:
+ - Inline mode: source.baseUrl = `https://localhost:8081/` (note trailing slash)
+ - IFrame API: playerVars.origin = `https://localhost:8081` (no trailing slash)
packages/react-native-youtube-bridge/src/hooks/useCreateLocalPlayerHtml.ts (2)

28-31: Origin fallback improved; also normalize and JS-escape the value.

Good call switching to || so an empty string falls back to localhost. Two follow-ups:

  • Normalize away trailing slashes to match the “no trailing slash” guidance.
  • Use JS-safe serialization (JSON.stringify) when inlining into the script, rather than HTML escaping, to avoid edge-case escaping issues inside JS strings.
-    const safeOrigin = escapeHtml(origin) || 'https://localhost';
+    // Normalize origin: trim and drop trailing slashes; fallback to localhost to keep in sync with baseUrl default.
+    const normalizedOrigin = (origin ?? '').trim().replace(/\/+$/, '');
+    const safeOrigin = normalizedOrigin || 'https://localhost';

And update the injection further below to serialize safely (outside this hunk):

// Before:
${safeOrigin ? `origin: '${safeOrigin}',` : ''}
// After (JS-escape):
${safeOrigin ? `origin: ${JSON.stringify(safeOrigin)},` : ''}

28-31: Optional: derive fallback origin from the inline baseUrl when provided.

Right now, if a user supplies a custom baseUrl but omits origin, we’ll still send https://localhost, causing a mismatch and disabled JS API. Consider deriving the fallback origin from the inline baseUrl (when present) in YoutubeView.tsx and passing it via playerVars.origin. I’ve proposed a minimal change on that file’s comment to avoid changing this hook’s signature.

packages/react-native-youtube-bridge/src/types/youtube.ts (1)

57-59: Doc update is clear; add one more constraint to prevent common pitfalls.

Recommend adding: “baseUrl must be an absolute http(s) URL (not file:// or content://) in inline mode so the IFrame origin check can succeed.”

  * In this case, the origin of `webViewUrl` MUST exactly match the YouTube IFrame API `origin`.
  * - Include the port when applicable (e.g. baseUrl `https://localhost:8081/` ⇄ origin `https://localhost:8081`).
  * - Use a trailing slash on the `baseUrl`, but not on `origin` (scheme + host [+ port] only).
+ * - baseUrl must be an absolute http(s) URL (not file:// or content://) so the IFrame API origin check can succeed.
packages/react-native-youtube-bridge/src/YoutubeView.tsx (1)

43-49: Optional: Derive and Pass Fallback Origin from webViewBaseUrl

The trailing-slash normalization for webViewBaseUrl and the default baseUrl (https://localhost/) look solid. However, if a consumer supplies a custom webViewBaseUrl but doesn’t include origin in their playerVars, the hook will fall back to https://localhost, leading to an origin mismatch and broken JS API calls.

• In YoutubeView.tsx (lines 43-49), when you call

const { videoId, playerVars } = useMemo(/* … */);

and then

const createPlayerHTML = useCreateLocalPlayerHtml({ videoId, useInlineHtml, ...playerVars });

consider injecting a derived origin whenever playerVars.origin is falsy.
• Example change:

-  const { videoId, playerVars } = useMemo(() => {
-    return { videoId: player.getVideoId(), playerVars: player.getOptions() || {} };
-  }, [player]);
+  const { videoId, playerVars } = useMemo(() => {
+    const options = player.getOptions() || {};
+    let derivedOrigin: string | undefined;
+    if (!options.origin && webViewBaseUrl) {
+      const base = webViewBaseUrl.endsWith('/') ? webViewBaseUrl : `${webViewBaseUrl}/`;
+      try {
+        const u = new URL(base);
+        derivedOrigin = `${u.protocol}//${u.host}`;
+      } catch {
+        // ignore invalid URL; will use https://localhost
+      }
+    }
+    return {
+      videoId: player.getVideoId(),
+      playerVars: { ...options, ...(derivedOrigin ? { origin: derivedOrigin } : {}) },
+    };
+  }, [player, webViewBaseUrl]);

This ensures the embedded player’s origin matches the actual WebView origin instead of defaulting to https://localhost.
No other default for origin is applied elsewhere in the codebase—both useCreateLocalPlayerHtml.ts and WebYoutubePlayerController.ts only pass through whatever ends up in playerVars.origin.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a4784e6 and 279d5bf.

📒 Files selected for processing (5)
  • .changeset/fresh-drinks-slide.md (1 hunks)
  • .changeset/slow-kings-lay.md (1 hunks)
  • packages/react-native-youtube-bridge/src/YoutubeView.tsx (2 hunks)
  • packages/react-native-youtube-bridge/src/hooks/useCreateLocalPlayerHtml.ts (1 hunks)
  • packages/react-native-youtube-bridge/src/types/youtube.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
.changeset/slow-kings-lay.md (3)
packages/react-native-youtube-bridge/src/modules/WebviewYoutubePlayerController.ts (2)
  • updateCallbacks (162-164)
  • WebviewYoutubePlayerController (4-170)
packages/react-native-youtube-bridge/src/YoutubeView.web.tsx (2)
  • YoutubeView (9-92)
  • state (47-49)
packages/web/src/YoutubePlayer.tsx (1)
  • YoutubePlayer (11-237)
packages/react-native-youtube-bridge/src/YoutubeView.tsx (2)
packages/react-native-youtube-bridge/src/modules/WebviewYoutubePlayerController.ts (2)
  • updateCallbacks (162-164)
  • WebviewYoutubePlayerController (4-170)
packages/web/src/YoutubePlayer.tsx (3)
  • YoutubePlayer (11-237)
  • message (136-223)
  • err (199-209)
.changeset/fresh-drinks-slide.md (3)
packages/react-native-youtube-bridge/src/YoutubeView.web.tsx (1)
  • YoutubeView (9-92)
packages/react-native-youtube-bridge/src/modules/WebviewYoutubePlayerController.ts (3)
  • updateCallbacks (162-164)
  • WebviewYoutubePlayerController (4-170)
  • constructor (9-11)
packages/web/src/YoutubePlayer.tsx (1)
  • YoutubePlayer (11-237)
packages/react-native-youtube-bridge/src/hooks/useCreateLocalPlayerHtml.ts (1)
packages/core/src/utils.ts (1)
  • escapeHtml (22-33)
packages/react-native-youtube-bridge/src/types/youtube.ts (2)
packages/react-native-youtube-bridge/src/YoutubeView.web.tsx (1)
  • YoutubeView (9-92)
packages/web/src/YoutubePlayer.tsx (1)
  • YoutubePlayer (11-237)
🔇 Additional comments (2)
.changeset/slow-kings-lay.md (1)

1-5: LGTM: patch metadata and doc note look good.

Changeset correctly captures the TSDoc adjustment; nothing to fix here.

packages/react-native-youtube-bridge/src/YoutubeView.tsx (1)

115-120: Dev-only logging improvement is helpful.

Adding the raw payload to the parse error log aids debugging without affecting production behavior.

@saseungmin saseungmin merged commit 3f6f6df into main Aug 25, 2025
5 checks passed
@saseungmin saseungmin deleted the fix/inline-origin-baseurl branch August 25, 2025 01:41
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