Skip to content

Conversation

@alexander-mart
Copy link
Member

@alexander-mart alexander-mart commented Jul 24, 2025

Summary by CodeRabbit

  • Chores
    • Updated dependencies to include a new package for enhanced URL handling.
    • Improved dynamic determination of the base URL for backend services to better reflect the current request context.

@coderabbitai
Copy link

coderabbitai bot commented Jul 24, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The package.json file was updated to include a new dependency, nextjs-current-url version ^1.0.3. The djangoBaseUrl export in src/shared/django-helpers.tsx was modified to use the getUrl function from this package for dynamic URL retrieval instead of a static environment variable.

Changes

File(s) Change Summary
package.json Added nextjs-current-url dependency at ^1.0.3
src/shared/django-helpers.tsx Changed djangoBaseUrl export to use getUrl({ req: context.req.host }) instead of env variable

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch alexander-mart-patch-4

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

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

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @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.

@netlify
Copy link

netlify bot commented Jul 24, 2025

Deploy Preview for dtp-stat failed. Why did it fail? →

Name Link
🔨 Latest commit d4eda2d
🔍 Latest deploy log https://app.netlify.com/projects/dtp-stat/deploys/6882a4509ddd1c0008366778

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: 2

🧹 Nitpick comments (1)
src/shared/django-helpers.tsx (1)

114-124: Architectural consideration: Client-side usage requires a different approach.

The djangoBaseUrl is used in client-side React components (e.g., DjangoRedirectOnIframeAuth). Since getUrl requires server-side request context, you'll need to:

  1. Pass the base URL from server-side props to client components
  2. Use a client-side method to determine the current URL (e.g., window.location.origin)
  3. Or maintain the environment variable approach for client-side usage

Consider whether dynamic URL resolution is truly needed for your use case, given the client-side constraints.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8547211 and a2f2b1a.

📒 Files selected for processing (1)
  • src/shared/django-helpers.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
src/shared/django-helpers.tsx (1)

Learnt from: alexander-mart
PR: #120
File: src/shared/django-helpers.tsx:15-16
Timestamp: 2025-07-24T20:42:58.703Z
Learning: В Next.js для получения текущего домена в SSR используется req.headers.host из объекта запроса. Для избежания CORS ошибок при работе с несколькими доменами, API запросы должны идти к тому же домену, что и текущая страница.

import Script from "next/script";
import * as React from "react";

import { getUrl } from 'nextjs-current-url';
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify if nextjs-current-url is the appropriate package for this use case.

Based on the retrieved learnings, Next.js SSR should use req.headers.host to get the current domain. Please verify that this package properly handles SSR contexts and provides the correct domain for avoiding CORS issues.


🌐 Web query:

What is the nextjs-current-url npm package and how does it work for getting URLs in Next.js SSR context?

💡 Result:

The nextjs-current-url npm package enables you to retrieve the current URL or route in Next.js applications, in both client-side and server-side environments—including in SSR (Server-Side Rendering) contexts[9].

How nextjs-current-url Works

  • Purpose:
    The package offers utility functions to quickly get the full URL, including protocol, host, path, and query string, regardless of whether your code runs on the server or client[9].

  • Client Side:
    On the client, it typically uses window.location to access the full URL.

  • Server Side (SSR):
    On the server, it extracts URL information from the request object (req) available in server-rendering methods like getServerSideProps or API routes[2]. This way, you can determine the full URL as seen by the user at request time, which is very useful for SSR scenarios.

Example Usage (SSR Context)

Suppose you use getServerSideProps in a page:

import { getCurrentUrl } from "nextjs-current-url";

export async function getServerSideProps(context) {
  const url = getCurrentUrl(context); // Retrieves full URL in SSR
  return { props: { url } };
}
  • Here, context contains the underlying Node.js req and res objects. The package reads from these to construct the correct URL, handling edge cases like proxies or Vercel deployments automatically[9].

When to Use

  • SSR Redirections
  • Canonical URL calculation
  • Generating absolute links or sharing URLs during server-rendered requests

Similar Alternatives

  • Manual construction with req object: You can build the URL manually from req.headers, but this requires boilerplate and does not handle all deployment edge cases automatically[2][1].
  • Other packages: Packages like nextjs-url provide a similar API for extracting protocol, host, and path from the request both on server and client[1].

Limitations and Notes

  • The package must be used inside SSR/server methods (like getServerSideProps, getInitialProps, or API routes) to access the req object. It will not work for static site generation (SSG), because at build time there is no incoming HTTP request[1].
  • For Next.js 13+ and App Router with server components, you may need to check compatibility or use alternative methods, as access to the full request object may differ[5][6].

In summary:
nextjs-current-url is a convenient utility for getting the entire current URL in Next.js, abstracting away the differences between client and server environments in SSR[9]. It is especially useful in dynamic, server-rendered pages where deducing the user's actual request URL is crucial.

Citations:


🏁 Script executed:

#!/bin/bash
# Locate django-helpers.tsx and show its content
fd django-helpers.tsx -x sed -n '1,200p'

Length of output: 5086


Fix SSR retrieval of the Django base URL

The nextjs-current-url package is perfectly capable of extracting the full URL in both client and SSR contexts, but it must be invoked inside a Next.js SSR lifecycle method (e.g. getServerSideProps) with the actual req object—not at module initialization with context.req.host, which doesn’t exist here. The current line will throw at build/runtime and won’t reliably prevent CORS issues.

• File: src/shared/django-helpers.tsx
• Line: definition of djangoBaseUrl

Suggested fix:

-// Module-scope (broken): no `context`, `req` is just a host string
-export const djangoBaseUrl = getUrl({ req: context.req.host }) ?? '';

+// pages/yourPage.tsx (or move to a custom SSR helper)
+export async function getServerSideProps(context) {
+  // Pass the full req object to getUrl
+  const djangoBaseUrl = getUrl({ req: context.req }) ?? '';
+  return { props: { djangoBaseUrl } };
+}
+
+// In your component, consume the prop instead of the top-level constant
+// export const djangoBaseUrl = props.djangoBaseUrl;

Alternatively, construct the base URL manually in SSR:

export async function getServerSideProps({ req }) {
  const protocol = req.headers['x-forwarded-proto'] || 'https';
  const host = req.headers.host;
  return { props: { djangoBaseUrl: `${protocol}://${host}` } };
}

• Ensure all downstream code (e.g. DjangoRedirectOnIframeAuth) reads djangoBaseUrl from props or context where SSR has run.
• Keep the import of getUrl if you move it into SSR—otherwise remove unused imports.
• This change guarantees you’re using the real request host (per req.headers.host) to avoid CORS issues during SSR.

🤖 Prompt for AI Agents
In src/shared/django-helpers.tsx at line 13, the current approach to define
djangoBaseUrl uses nextjs-current-url at module initialization, which fails in
SSR because context.req.host is unavailable. To fix this, remove the
djangoBaseUrl definition from the module scope and instead obtain the base URL
inside a Next.js SSR lifecycle method like getServerSideProps by extracting
protocol and host from req.headers, then pass djangoBaseUrl as a prop or via
context to downstream components. Also, remove the getUrl import if it is no
longer used outside SSR.

Comment on lines +17 to +18
// export const djangoBaseUrl = process.env.NEXT_PUBLIC_DJANGO_BASE_URL ?? "";
export const djangoBaseUrl = getUrl({ req: context.req.host }) ?? '';
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Critical: context is undefined and implementation is incorrect for module-level export.

The current implementation has several critical issues:

  1. context is not defined anywhere, which will cause a ReferenceError
  2. djangoBaseUrl is a module-level export that cannot access request context
  3. The syntax getUrl({ req: context.req.host }) appears incorrect

This change from a static environment variable to dynamic URL retrieval fundamentally changes how djangoBaseUrl can be used. You'll need to either:

  • Keep it as an environment variable for module-level usage
  • Refactor all usages to pass the request context and compute the URL dynamically

Let me verify how djangoBaseUrl is currently used in the codebase:


🏁 Script executed:

#!/bin/bash
# Description: Check all usages of djangoBaseUrl to understand the refactoring scope

# Search for all imports and usages of djangoBaseUrl
rg -A 3 "djangoBaseUrl" --type tsx --type ts

# Check if there are any Next.js API routes or getServerSideProps that could provide context
fd -e tsx -e ts -x grep -l "getServerSideProps\|getStaticProps\|export.*async.*function.*handler" {} \;

Length of output: 181


🏁 Script executed:

#!/bin/bash
# Locate the django-helpers file
file_path=$(fd -t f -e tsx -e ts "django-helpers.tsx" . | head -n 1)
echo "Found file: $file_path"

# Show the top of the file to verify 'context' usage
if [[ -n "$file_path" ]]; then
  sed -n '1,200p' "$file_path"
else
  echo "django-helpers.tsx not found"
fi

# Search for all references to djangoBaseUrl in the repo
rg "djangoBaseUrl" -n .

# Search for Next.js data-fetching methods or API route handlers that might provide context
rg -n "getServerSideProps|getStaticProps|export\\s+default\\s+function\\s+handler" .

Length of output: 6903


Critical: context is undefined and cannot be used at module level

The export on line 18 of src/shared/django-helpers.tsx

export const djangoBaseUrl = getUrl({ req: context.req.host }) ?? '';

will throw a ReferenceError because context is never declared—and at module load time there is no Next.js request context available. All of the 10+ import sites (e.g. src/pages/api/rewrites/proxy-django-html-page.tsx, src/components/inherited-map/api.ts, etc.) will break.

You must either:

  • Revert to using the environment variable for module-level code:
    - export const djangoBaseUrl = getUrl({ req: context.req.host }) ?? '';
    + export const djangoBaseUrl = process.env.NEXT_PUBLIC_DJANGO_BASE_URL ?? '';
  • Or refactor djangoBaseUrl into a function that accepts the Next.js request (or context) at runtime, for example:
    export function getDjangoBaseUrl(req: NextApiRequest | { host: string }): string {
      return getUrl({ req }) ?? process.env.NEXT_PUBLIC_DJANGO_BASE_URL ?? '';
    }
    and update all call sites to invoke getDjangoBaseUrl(req) instead of importing a top-level constant.
🤖 Prompt for AI Agents
In src/shared/django-helpers.tsx at lines 17-18, the export of djangoBaseUrl
uses an undefined variable context at the module level, causing a
ReferenceError. To fix this, remove the module-level usage of context and either
revert djangoBaseUrl to use the environment variable directly or refactor it
into a function that accepts the request or context at runtime. Update all
import sites to call this new function with the appropriate request object
instead of importing a constant.

@alexander-mart alexander-mart deleted the alexander-mart-patch-4 branch July 24, 2025 21:31
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