Add author filtering and CORS proxy for latest rules#804
Conversation
There was a problem hiding this comment.
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
RulesWidgetto fetchpeople-latest-rules.json, normalize the providedauthor, and select/sort rules for that author. - Add a dev-only proxy route (
/__rules/people-latest-rules.json) ingatsby-node.jsthat streams the upstream JSON to localhost duringgatsby 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)declaresebut never uses it. This triggersno-unused-varswarnings, and the repo lint script uses--max-warnings=0(so the build will fail). Usecatch { ... }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) { |
There was a problem hiding this comment.
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.
| } catch (e) { | |
| } catch { |
gatsby-node.js
Outdated
| res.set('X-SSW-People-Proxy', 'axios'); | ||
|
|
||
| upstream.data.pipe(res); | ||
| } catch (e) { |
There was a problem hiding this comment.
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).
| } catch (e) { | |
| } catch (e) { | |
| console.error('Failed to proxy rules JSON', e); |
gatsby-node.js
Outdated
| upstream.data.pipe(res); | ||
| } catch (e) { | ||
| res.status(500).json({ error: 'Failed to proxy rules JSON' }); |
There was a problem hiding this comment.
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.
| 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(); | |
| } |
…ables and redundant comments, improving code readability.
This pull request enhances the
RulesWidgetcomponent to improve how it fetches and displays the latest rules for a given author.Development environment improvements:
onCreateDevServerhandler ingatsby-node.jsto proxy requests forpeople-latest-rules.jsonduring development, avoiding CORS issues by streaming the file from the production server.axiosdependency to support HTTP requests in the dev server proxy.RulesWidget data handling and robustness:
RulesWidgetto fetch rule data from the newpeople-latest-rules.jsonendpoint, using the dev proxy path in development and the production URL otherwise.normalizeAuthorandpickRulesForAuthorhelper functions to standardize author identifiers and reliably extract rules for the correct user from the JSON data, handling various input formats.