Skip to content

Commit

Permalink
docs: add new mechanism for api key in examples (#162)
Browse files Browse the repository at this point in the history
Adds a new mechanism that allows us to use an API-key from github-secrets exclusively when examples are opened in a codesandbox from the links on our website.
  • Loading branch information
mrMetalWood committed Jan 18, 2024
1 parent 7309efa commit 6ecd7a9
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ jobs:
working-directory: ./website
run: npm ci


- name: Build website
working-directory: ./website
run: |
npm run build
env:
GOOGLE_MAPS_API_KEY: ${{ secrets.GOOGLE_MAPS_API_KEY }}

- name: Find and Replace - Examples API Key
uses: jacobtomlinson/gha-find-replace@v3
with:
find: 'process.env.GOOGLE_MAPS_API_KEY'
replace: ${{ secrets.GOOGLE_MAPS_API_KEY_EXAMPLES }}
include: './website/build/scripts/examples.js'

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
Expand Down
1 change: 1 addition & 0 deletions examples/basic-map/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<div id="app"></div>
<script type="module">
import '@vis.gl/react-google-maps/examples.css';
import '@vis.gl/react-google-maps/examples.js';
import {renderToDom} from './src/app';

renderToDom(document.querySelector('#app'));
Expand Down
3 changes: 2 additions & 1 deletion examples/basic-map/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {createRoot} from 'react-dom/client';
import {APIProvider, Map} from '@vis.gl/react-google-maps';
import ControlPanel from './control-panel';

const API_KEY = process.env.GOOGLE_MAPS_API_KEY as string;
const API_KEY =
globalThis.GOOGLE_MAPS_API_KEY ?? (process.env.GOOGLE_MAPS_API_KEY as string);

const App = () => (
<APIProvider apiKey={API_KEY}>
Expand Down
6 changes: 6 additions & 0 deletions examples/basic-map/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export default defineConfig(({mode}) => {
return {
define: {
'process.env.GOOGLE_MAPS_API_KEY': JSON.stringify(GOOGLE_MAPS_API_KEY)
},
resolve: {
alias: {
'@vis.gl/react-google-maps/examples.js':
'https://visgl.github.io/react-google-maps/scripts/examples.js'
}
}
};
});
5 changes: 5 additions & 0 deletions examples/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare global {
// const or let does not work in this case, it has to be var
// eslint-disable-next-line no-var
var GOOGLE_MAPS_API_KEY: string | undefined;
}
3 changes: 3 additions & 0 deletions examples/vite.config.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default defineConfig(({mode}) => {
},
resolve: {
alias: {
'@vis.gl/react-google-maps/examples.js': resolve(
'../../website/static/scripts/examples.js'
),
'@vis.gl/react-google-maps/examples.css': resolve(
'../../examples/examples.css'
),
Expand Down
4 changes: 4 additions & 0 deletions website/static/scripts/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// For the codesandbox examples, the 'process.env.GOOGLE_MAPS_API_KEY'
// gets replaced in the deploy task in the github action
// to contain the valid key for the examples
globalThis.GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY;

0 comments on commit 6ecd7a9

Please sign in to comment.