Skip to content

Add author filtering and CORS proxy for latest rules#804

Merged
PothieuG merged 2 commits intomainfrom
2468-fetch-latest-rules
Mar 3, 2026
Merged

Add author filtering and CORS proxy for latest rules#804
PothieuG merged 2 commits intomainfrom
2468-fetch-latest-rules

Conversation

@Aibono1225
Copy link
Member

@Aibono1225 Aibono1225 commented Mar 3, 2026

This pull request enhances the RulesWidget component to improve how it fetches and displays the latest rules for a given author.

Development environment improvements:

  • Added an onCreateDevServer handler in gatsby-node.js to proxy requests for people-latest-rules.json during development, avoiding CORS issues by streaming the file from the production server.
  • Added the axios dependency to support HTTP requests in the dev server proxy.

RulesWidget data handling and robustness:

  • Refactored RulesWidget to fetch rule data from the new people-latest-rules.json endpoint, using the dev proxy path in development and the production URL otherwise.
  • Introduced normalizeAuthor and pickRulesForAuthor helper functions to standardize author identifiers and reliably extract rules for the correct user from the JSON data, handling various input formats.
  • Updated the widget to filter, sort, and limit the displayed rules per author, and to consistently use the normalized author for all lookups and links. [1] [2]
image

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds author-based filtering for “Latest Rules” and introduces a Gatsby dev-server proxy endpoint to avoid CORS issues when developing locally.

Changes:

  • Switch RulesWidget to fetch people-latest-rules.json, normalize the provided author, and select/sort rules for that author.
  • Add a dev-only proxy route (/__rules/people-latest-rules.json) in gatsby-node.js that streams the upstream JSON to localhost during gatsby develop.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/components/rules-widget/rules-widget.js Fetches the shared “people latest rules” JSON and filters it down to an author-specific list for display.
gatsby-node.js Adds a onCreateDevServer route to proxy the rules JSON in development to bypass browser CORS.
Comments suppressed due to low confidence (1)

src/components/rules-widget/rules-widget.js:102

  • catch (e) declares e but never uses it. This triggers no-unused-vars warnings, and the repo lint script uses --max-warnings=0 (so the build will fail). Use catch { ... } or handle/log the error variable.
            } catch (e) {
                if (!cancelled) {
                    setItems([]);
                    setStatus("error");
                }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const u = new URL(raw);
const last = u.pathname.split("/").filter(Boolean).pop();
return (last || "").trim();
} catch (e) {
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

catch (e) declares e but never uses it. With the repo's no-unused-vars set to warn and eslint run with --max-warnings=0, this will fail lint. Use optional catch binding (catch { ... }) or reference the error variable (e.g. log it) to avoid the unused var warning.

Suggested change
} catch (e) {
} catch {

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

gatsby-node.js Outdated
res.set('X-SSW-People-Proxy', 'axios');

upstream.data.pipe(res);
} catch (e) {
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

catch (e) declares e but never uses it, which triggers no-unused-vars warnings. Since the repo runs eslint with --max-warnings=0, this will fail lint. Prefer catch { ... } or otherwise use the error variable (e.g. include it in the response/logging).

Suggested change
} catch (e) {
} catch (e) {
console.error('Failed to proxy rules JSON', e);

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

gatsby-node.js Outdated
Comment on lines +641 to +643
upstream.data.pipe(res);
} catch (e) {
res.status(500).json({ error: 'Failed to proxy rules JSON' });
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

upstream.data.pipe(res) doesn’t attach error/close handlers. If the upstream stream errors or the client disconnects, this can emit an unhandled stream error (or keep the upstream request open). Consider wiring upstream.data.on('error', …) and req.on('close', …) / res.on('close', …) to abort the upstream request and end the response cleanly.

Suggested change
upstream.data.pipe(res);
} catch (e) {
res.status(500).json({ error: 'Failed to proxy rules JSON' });
const cleanup = () => {
const stream = upstream && upstream.data;
if (stream && typeof stream.destroy === 'function' && !stream.destroyed) {
stream.destroy();
}
};
req.on('close', cleanup);
res.on('close', cleanup);
upstream.data.on('error', (err) => {
cleanup();
if (!res.headersSent) {
res
.status(502)
.json({ error: 'Failed to proxy rules JSON from upstream' });
} else {
// If headers are already sent, just end the response.
res.end();
}
});
upstream.data.pipe(res);
} catch (e) {
if (!res.headersSent) {
res.status(500).json({ error: 'Failed to proxy rules JSON' });
} else {
res.end();
}

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

…ables and redundant comments, improving code readability.
@PothieuG PothieuG merged commit 609ab41 into main Mar 3, 2026
2 checks passed
@PothieuG PothieuG deleted the 2468-fetch-latest-rules branch March 3, 2026 13:01
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