Skip to content

[WIP] Optimize performance and UX for shortlink project - #2

Merged
zhikanyeye merged 1 commit into
mainfrom
copilot/optimize-shortlink-project-again
Dec 4, 2025
Merged

[WIP] Optimize performance and UX for shortlink project#2
zhikanyeye merged 1 commit into
mainfrom
copilot/optimize-shortlink-project-again

Conversation

Copilot AI commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Optimize the Cloudflare Worker shortlink project based on the following requirements:

  1. Performance & Cost Optimization:

    • Remove the cleanupExpiredUrls function and the scheduled event handler. Cloudflare KV's native TTL (expirationTtl) handles expiration automatically, making manual cleanup redundant and costly.
    • Remove the access counting logic (updating accessCount and lastAccessed in KV) during redirects. KV is not designed for high-frequency writes (counters), and this slows down redirects and risks write conflicts.
  2. Feature & UX Optimization:

    • Auto-generate Short Codes: Make the "Custom Path" (自定义后缀) optional in the frontend.
    • Update the backend (/create endpoint): If customPath is not provided, generate a random 6-character alphanumeric string. ensure it doesn't conflict with existing keys (simple retry logic or just check once).
    • Relax URL Validation: Modify isValidTargetUrl. Instead of failing on timeout/error, treat the URL as valid if the format is correct but the fetch check fails or times out. Reduce the timeout to 2 seconds. Log the warning but allow creation.
  3. Code Structure:

    • Keep the HTML inside worker.js for now (to keep the single-file structure simple for the user), but update the HTML string to reflect the frontend changes (removing required attribute from the custom path input, updating placeholders).
  4. Specific Code Changes:

    • Frontend (HTML string):
      • Remove required attribute from <input id="customPath">.
      • Update placeholder to indicate it's optional (e.g., "Optional: Leave empty for random").
      • Update JS validation in the HTML to allow empty customPath.
    • Backend (worker.js):
      • Delete cleanupExpiredUrls function.
      • Delete scheduled export.
      • In isValidTargetUrl: Reduce timeout to 2000ms. catch errors and return true (allowance) instead of false, perhaps just console.log the error.
      • In /create handler:
        • If data.customPath is empty, generate a random string (e.g., Math.random().toString(36).substring(2, 8)).
        • Ensure the random path doesn't exist (check KV).
      • In Redirect handler (path lookup):
        • Remove the code block that updates accessCount and writes back to KV. Just return Response.redirect.

File to modify: worker.js

This pull request was created as a result of the following prompt from Copilot chat.

Optimize the Cloudflare Worker shortlink project based on the following requirements:

  1. Performance & Cost Optimization:

    • Remove the cleanupExpiredUrls function and the scheduled event handler. Cloudflare KV's native TTL (expirationTtl) handles expiration automatically, making manual cleanup redundant and costly.
    • Remove the access counting logic (updating accessCount and lastAccessed in KV) during redirects. KV is not designed for high-frequency writes (counters), and this slows down redirects and risks write conflicts.
  2. Feature & UX Optimization:

    • Auto-generate Short Codes: Make the "Custom Path" (自定义后缀) optional in the frontend.
    • Update the backend (/create endpoint): If customPath is not provided, generate a random 6-character alphanumeric string. ensure it doesn't conflict with existing keys (simple retry logic or just check once).
    • Relax URL Validation: Modify isValidTargetUrl. Instead of failing on timeout/error, treat the URL as valid if the format is correct but the fetch check fails or times out. Reduce the timeout to 2 seconds. Log the warning but allow creation.
  3. Code Structure:

    • Keep the HTML inside worker.js for now (to keep the single-file structure simple for the user), but update the HTML string to reflect the frontend changes (removing required attribute from the custom path input, updating placeholders).
  4. Specific Code Changes:

    • Frontend (HTML string):
      • Remove required attribute from <input id="customPath">.
      • Update placeholder to indicate it's optional (e.g., "Optional: Leave empty for random").
      • Update JS validation in the HTML to allow empty customPath.
    • Backend (worker.js):
      • Delete cleanupExpiredUrls function.
      • Delete scheduled export.
      • In isValidTargetUrl: Reduce timeout to 2000ms. catch errors and return true (allowance) instead of false, perhaps just console.log the error.
      • In /create handler:
        • If data.customPath is empty, generate a random string (e.g., Math.random().toString(36).substring(2, 8)).
        • Ensure the random path doesn't exist (check KV).
      • In Redirect handler (path lookup):
        • Remove the code block that updates accessCount and writes back to KV. Just return Response.redirect.

File to modify: worker.js


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

Copilot AI requested a review from zhikanyeye December 4, 2025 13:40
@zhikanyeye zhikanyeye closed this Dec 4, 2025
@zhikanyeye zhikanyeye reopened this Dec 4, 2025
@zhikanyeye
zhikanyeye marked this pull request as ready for review December 4, 2025 14:18
@zhikanyeye
zhikanyeye merged commit 8f443aa into main Dec 4, 2025
1 check failed
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