Skip to content

feat: redirect from data script#619

Merged
surajshetty3416 merged 2 commits into
frappe:developfrom
pratikb64:feat/data-script-redirect
Jun 3, 2026
Merged

feat: redirect from data script#619
surajshetty3416 merged 2 commits into
frappe:developfrom
pratikb64:feat/data-script-redirect

Conversation

@pratikb64

Copy link
Copy Markdown
Contributor

Redirect visitors to a URL from a Data Script

What

Adds the ability to redirect a page visitor to any URL from within a page Data Script.

# example
redirect("/login")          # 302 by default
redirect("/login", 301)   # optional custom status code

Why

Data scripts already run server-side on every render and can branch on the
session/user, but there was no way to send the visitor elsewhere. This closes that gap with a one-line helper.

How

  • Injects a redirect(location, http_status_code=302) helper into the page
    data script namespace. When called during an actual page render it sets
    frappe.local.flags.redirect_location and raises frappe.Redirect, which the
    Frappe website renderer turns into a real HTTP redirect — entirely
    server-side

Security

  • get_page_data is split into a whitelisted wrapper (always
    for_render=False) and an internal _get_page_data(for_render=True) used
    only by the render path — so a client cannot trigger the redirect via the API.
  • Pages with a page_data_script are now marked no_cache. A data script can
    branch on the user/session, so without this a render for one user could be
    cached and served to another, bypassing a conditional redirect (auth-bypass).
  • Redirect targets come from author-written server-side Python (same trust
    level as any data script); external URLs are intentionally allowed.

Tests

  • test_redirect_from_page_data_script: asserts the rendered response is a
    302 to the target URL, and that the editor's get_page_data() call is a
    no-op returning empty data.

@codecov-commenter

codecov-commenter commented Jun 2, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.63%. Comparing base (7f03261) to head (a63b47f).
⚠️ Report is 1 commits behind head on develop.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #619      +/-   ##
===========================================
+ Coverage    58.41%   58.63%   +0.21%     
===========================================
  Files           29       29              
  Lines         3239     3256      +17     
===========================================
+ Hits          1892     1909      +17     
  Misses        1347     1347              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pratikb64 pratikb64 requested a review from surajshetty3416 June 2, 2026 12:35
@surajshetty3416 surajshetty3416 merged commit 5fc3c2f into frappe:develop Jun 3, 2026
7 of 8 checks passed
@surajshetty3416

surajshetty3416 commented Jun 3, 2026

Copy link
Copy Markdown
Member

@pratikb64 Update docs as well?

https://docs.frappe.io/builder/data-script

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.

3 participants