gatsby-remark-strava
v2.0.0
Published
Gatsby remark plugin to embed Strava activities and routes by their URL
Maintainers
Readme
Gatsby remark plugin to embed Strava activities and routes by their URL.
It works in two modes, chosen by its options:
- Embed mode, the default, shows the official Strava embed. It needs no token and makes no request.
- Render mode, when you give a
renderfunction, builds your own markup from the activity. The activity comes from the store of strava-sync, the onegatsby-source-stravafills, and is fetched from Strava only when it is missing.
Requires Node 22.12 or later.
Usage
- Download
gatsby-remark-stravafrom the NPM registry:
npm install gatsby-remark-strava- Add the plugin to your
gatsby-config.jsfile. It must be a child plugin ofgatsby-transformer-remark, or agatsbyRemarkPluginsentry ofgatsby-plugin-mdx.
module.exports = {
plugins: [
{
resolve: "gatsby-transformer-remark",
options: {
plugins: ["gatsby-remark-strava"],
},
},
],
}- Add a link to your content, alone in its paragraph:
[strava activity 3657585594 embed](https://www.strava.com/activities/3657585594)
[strava route 3350146254546787420 embed](https://www.strava.com/routes/3350146254546787420)The link text must contain "embed" (whatever the case), so that a plain link to an activity stays a link. The URL is https://www.strava.com/activities/<id> or https://www.strava.com/routes/<id>, with an optional trailing slash or query.
Embed mode
The link becomes the placeholder of the official embed, and a small script of the plugin loads https://strava-embeds.com/embed.js on the pages that have one. The script replaces each placeholder with an iframe, also when the page is reached by a client side navigation.
<div
class="strava-embed-placeholder"
data-embed-type="activity"
data-embed-id="3657585594"
></div>Strava only shows what is public:
- an activity needs its profile and its visibility set to Everyone,
- a route needs to be public.
Segments are not supported.
Render mode
Give a render function that receives the activity and returns the markup. Routes keep the official embed.
module.exports = {
plugins: [
{
resolve: "gatsby-plugin-mdx",
options: {
gatsbyRemarkPlugins: [
{
resolve: "gatsby-remark-strava",
options: {
output: "mdx",
storeDir: ".strava",
stravaClientId: process.env.STRAVA_CLIENT_ID,
stravaClientSecret: process.env.STRAVA_CLIENT_SECRET,
stravaToken: process.env.STRAVA_TOKEN,
render: (activity) => {
const date = new Date(activity.start_date).toLocaleDateString(
"en-us",
{year: "numeric", month: "short", day: "numeric"}
)
return `
<strava
id="${activity.id}"
name="${activity.name}"
distance="${activity.distance}"
start_date="${date}"
photo="${activity.photos?.primary?.urls["600"] || ""}"
/>
`
},
},
},
],
},
},
],
}renderalways receives a detailed activity, the shape ofGET /activities/{id}(calories,photos.primary.urls...). The activity is read from thestoreDirfolder first, so a site that usesgatsby-source-stravamakes no request for the activities it has stored with their details (resource_state3).- The API is called, with
strava-sync, for an activity that is missing from the store or that the store only holds as a summary of the list (resource_state2). It needsstravaClientId,stravaClientSecretandstravaToken. Up to 4 requests run at once, and the activity is kept in the Gatsby cache, so the next builds do not fetch it again. - Without the token options, a summary of the store is passed to
renderas it is, with one warning, and a missing activity stays a link. - When
gatsby-source-stravastores the list of photos,photosis that list and the summary is underphotos_summary.rendergets the shape of the API, with the summary back underphotos. - Generate the token with
npx strava-sync token, see the strava-sync documentation.
Usage with MDX
gatsby-plugin-mdx turns raw HTML nodes into inert <span dangerouslySetInnerHTML> elements. Set output: "mdx" to emit JSX elements instead, in both modes.
With output: "mdx", the markup returned by render must be a single JSX element, so use className instead of class and an object for style. Every tag goes through the components given to MDXProvider, so a custom tag such as <strava> can be mapped to a React component. A capitalized tag must be provided there, otherwise MDX throws.
See the remark example for the embed mode, and the mdx one for the render mode.
Options
| Option | Default | Description |
| -------------------- | ----------- | ------------------------------------------------------------------------------------ |
| render | | Function receiving the activity and returning the markup. It switches to render mode |
| output | "html" | "html" for gatsby-transformer-remark, "mdx" for gatsby-plugin-mdx (MDX v2+) |
| storeDir | ".strava" | Store folder, relative to the site, the same as the gatsby-source-strava option |
| stravaClientId | | Strava application client ID, to fetch an activity missing from the store |
| stravaClientSecret | | Strava application client secret |
| stravaToken | | Strava token, as generated by strava-sync token |
| debug | false | Logs every embed that succeeds |
A link that cannot be turned into an embed, because of a failing render or a missing activity, stays a link and logs a warning.
Migrating from 1.x
- The iframe
https://www.strava.com/activities/<id>/embed/<token>stopped displaying, Strava no longer supports it. The default is now the official embed, which needs a public activity and no token. Remove the three Strava options if you do not userender. - Routes links are embedded too.
- The
gatsby-remark-strava-tokencommand is gone. Runnpx strava-sync tokeninstead, it writes the sameSTRAVA_CLIENT_ID,STRAVA_CLIENT_SECRETandSTRAVA_TOKENvariables. renderis kept, and now switches to render mode. The activity is read from thegatsby-source-stravastore before Strava is called, so the token is only needed for an activity that is not in it, or that it only holds as a summary. SetstoreDirif you moved the store.outputanddebugare kept. The fetched activity is cached, not the markup.- Node 22.12 or later is required.
