Skip to content

Fix: preview URL#966

Merged
hexqi merged 3 commits intoopentiny:refactor/developfrom
yy-wow:fix/preview-production
Dec 27, 2024
Merged

Fix: preview URL#966
hexqi merged 3 commits intoopentiny:refactor/developfrom
yy-wow:fix/preview-production

Conversation

@yy-wow
Copy link
Collaborator

@yy-wow yy-wow commented Dec 27, 2024

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

预览跳转URL拼接出错,当前是通过获取window.location.href.split('?')[0]后拼接"/preview"
示例:https://xxx.com/?type=app&id=918
跳转时拼接为:https://xxx.com//preview?xxxxxxx。多余的"/"。

Issue Number: N/A

What is the new behavior?

兼容"/?"和"?"的URL

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • Bug Fixes
    • Improved URL formatting in the open function to ensure correct concatenation of the href and preview paths, preventing issues with multiple slashes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 27, 2024

Walkthrough

The pull request modifies the open function in the preview.js file, specifically addressing how URLs are constructed when not in a development environment. The change ensures consistent URL formatting by carefully handling the slash between the base href and the /preview path, preventing potential double-slash issues during URL generation.

Changes

File Change Summary
packages/common/js/preview.js Modified URL construction logic in open function to ensure consistent slash placement between href and /preview

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • chilingling
  • lhuans

Poem

🐰 A slash so neat, a URL so clean,
No double slashes to be seen!
With precision we append the way,
Preview links dancing in array,
Code rabbit hops with glee supreme! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5774bde and 7f3e87c.

📒 Files selected for processing (1)
  • packages/common/js/preview.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/common/js/preview.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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.

@yy-wow yy-wow added ready_for_review This PR requires more reviews and removed refactor-main labels Dec 27, 2024
Copy link
Contributor

@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

🔭 Outside diff range comments (1)
packages/common/js/preview.js (1)

Line range hint 21-24: Add input validation for URL parameters.

The tenant and app ID parameters are used without validation. Consider adding checks to prevent potential security issues.

+  const validateParam = (param, name) => {
+    if (param && !/^[a-zA-Z0-9-_]+$/.test(param)) {
+      console.error(`Invalid ${name} parameter`)
+      return null
+    }
+    return param
+  }
   const paramsMap = new URLSearchParams(location.search)
-  params.app = paramsMap.get('id')
-  params.tenant = paramsMap.get('tenant')
+  params.app = validateParam(paramsMap.get('id'), 'app')
+  params.tenant = validateParam(paramsMap.get('tenant'), 'tenant')
🧹 Nitpick comments (3)
packages/common/js/preview.js (3)

Line range hint 31-31: Consider adding URL validation and error handling.

The current fallback to './' might not be sufficient in all cases. Consider adding validation for the href value and proper error handling.

-  const href = window.location.href.split('?')[0] || './'
+  const href = window.location.href.split('?')[0]
+  if (!href) {
+    console.warn('Invalid URL: falling back to default path')
+    return `./preview?tenant=${tenant}#${hashString}`
+  }

Also applies to: 45-45


Line range hint 26-31: Consider caching script dependencies.

The script dependencies are filtered and transformed on every preview. Consider caching this transformation if the dependencies don't change frequently.


Line range hint 53-61: Add JSDoc documentation for exported functions.

The exported functions lack documentation. Consider adding JSDoc comments to improve code maintainability.

+/**
+ * Opens a preview of a page in a new tab
+ * @param {Object} params - Preview configuration parameters
+ * @param {string} params.app - Application ID
+ * @param {string} params.tenant - Tenant ID
+ */
 export const previewPage = (params = {}) => {
   params.type = COMPONENT_NAME.Page
   open(params)
 }

+/**
+ * Opens a preview of a block in a new tab
+ * @param {Object} params - Preview configuration parameters
+ * @param {string} params.app - Application ID
+ * @param {string} params.tenant - Tenant ID
+ */
 export const previewBlock = (params = {}) => {
   params.type = COMPONENT_NAME.Block
   open(params)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7716dda and 5774bde.

📒 Files selected for processing (1)
  • packages/common/js/preview.js (1 hunks)
🔇 Additional comments (2)
packages/common/js/preview.js (2)

45-45: LGTM! URL construction fix looks correct.

The solution elegantly handles both URL formats (with and without trailing slash) by conditionally adding the separator. This fixes the double slash issue mentioned in the PR description.


45-45: Verify preview URL construction across the codebase.

Let's ensure there are no other instances of preview URL construction that might need similar fixes.

✅ Verification successful

Let me try another verification script with corrected patterns:


Preview URL construction is consistent across the codebase

Based on the verification results, there are only two instances of preview URL construction in the codebase:

  • packages/common/js/preview.js: The file under review with preview?tenant=${tenant}#${hashString}
  • packages/toolbars/logo/src/Main.vue: Using a different pattern previewApp?appid=${appId}&tenant=${tenantId}

These are separate use cases with different URL structures and parameters, serving different purposes. The changes made to the preview URL construction in preview.js do not need to be replicated elsewhere as the other instance follows a different pattern for a different feature.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other instances of preview URL construction
rg -l "preview\?" .
rg -l "\/preview" .

# Search for usage of the preview functions to ensure compatibility
ast-grep --pattern 'previewPage($$$)' 
ast-grep --pattern 'previewBlock($$$)'

Length of output: 232


Script:

#!/bin/bash
# Search for other instances of preview URL construction with fixed patterns
rg -l "/preview" .
rg -l "preview\?" .

# Search for preview-related function calls and URL constructions
rg -A 3 "preview[\?\/]" .

# Look for any URL construction patterns that might be related
rg -A 3 "href.*preview" .

Length of output: 1688

rhlin
rhlin previously approved these changes Dec 27, 2024
@hexqi hexqi merged commit 9027902 into opentiny:refactor/develop Dec 27, 2024
@coderabbitai coderabbitai bot mentioned this pull request Mar 28, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready_for_review This PR requires more reviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants