Skip to content

Commit 98fc856

Browse files
iamjr15claude
andcommitted
fix: use absolute API URL for external embedding support
- Changed API_BASE to absolute URL so widget works when embedded externally - Updated React/Vue integration examples to include CSS loading - Fixed framework examples with proper cleanup on unmount 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 87e6911 commit 98fc856

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,28 @@ The widget modifies the DOM directly, so you must use `useEffect` to initialize
163163
```jsx
164164
import { useEffect } from 'react';
165165

166-
// 1. Import the CSS (or copy the contents to your CSS file)
167-
// import './gh.css';
168-
169166
const ContributionGraph = ({ username }) => {
170167
useEffect(() => {
171-
// 2. Load the script dynamically
168+
// Add CSS
169+
const link = document.createElement('link');
170+
link.rel = 'stylesheet';
171+
link.href = 'https://github-contribution-graph.netlify.app/assets/css/gh.css';
172+
document.head.appendChild(link);
173+
174+
// Add Script
172175
const script = document.createElement('script');
173-
script.src = "https://github-contribution-graph.netlify.app/assets/js/gh.js";
176+
script.src = 'https://github-contribution-graph.netlify.app/assets/js/gh.js';
174177
script.async = true;
175178
document.body.appendChild(script);
176179

177180
return () => {
181+
// Cleanup on unmount
182+
document.head.removeChild(link);
178183
document.body.removeChild(script);
179-
}
184+
};
180185
}, []);
181186

182-
return (
183-
<div className="main-container">
184-
{/* Your wrapper elements... */}
185-
<div id="gh" data-login={username}></div>
186-
</div>
187-
);
187+
return <div id="gh" data-login={username}></div>;
188188
};
189189

190190
export default ContributionGraph;
@@ -194,24 +194,36 @@ export default ContributionGraph;
194194

195195
```vue
196196
<template>
197-
<div class="main-container">
198-
<div id="gh" :data-login="username"></div>
199-
</div>
197+
<div id="gh" :data-login="username"></div>
200198
</template>
201199
202200
<script setup>
203-
import { onMounted } from 'vue';
201+
import { onMounted, onUnmounted } from 'vue';
204202
205203
const props = defineProps({
206204
username: String
207205
});
208206
207+
let link, script;
208+
209209
onMounted(() => {
210-
const script = document.createElement('script');
211-
script.src = "https://github-contribution-graph.netlify.app/assets/js/gh.js";
210+
// Add CSS
211+
link = document.createElement('link');
212+
link.rel = 'stylesheet';
213+
link.href = 'https://github-contribution-graph.netlify.app/assets/css/gh.css';
214+
document.head.appendChild(link);
215+
216+
// Add Script
217+
script = document.createElement('script');
218+
script.src = 'https://github-contribution-graph.netlify.app/assets/js/gh.js';
212219
script.async = true;
213220
document.body.appendChild(script);
214221
});
222+
223+
onUnmounted(() => {
224+
if (link) document.head.removeChild(link);
225+
if (script) document.body.removeChild(script);
226+
});
215227
</script>
216228
```
217229

assets/js/gh.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
// API base URL - uses absolute URL for external embedding support
2+
const API_BASE = 'https://github-contribution-graph.netlify.app';
3+
14
async function fetchData(ghLogin) {
25
try {
3-
const response = await fetch(`/api/ghcg/fetch-data?login=${ghLogin}`);
6+
const response = await fetch(`${API_BASE}/api/ghcg/fetch-data?login=${ghLogin}`);
47
if (!response.ok) {
58
throw new Error(`HTTP error! status: ${response.status}`);
69
}

0 commit comments

Comments
 (0)