Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding index.tsx with the third party components #3

Open
wants to merge 2 commits into
base: tpc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/third-parties/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# `@next/third-parties`

Choose a reason for hiding this comment

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

Let's add a note at the top that says this is still experimental or early in development, just so folks are aware :)

`@next/third-parties` is collection of components that can be used to efficiently load third party libraries into your Next.js application.
This package is the Next.js implementation of the [Third Party Capital](https://github.com/GoogleChromeLabs/third-party-capital/) loading recommendations.

Choose a reason for hiding this comment

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

Suggested change
This package is the Next.js implementation of the [Third Party Capital](https://github.com/GoogleChromeLabs/third-party-capital/) loading recommendations.
This package uses [Third Party Capital](https://github.com/GoogleChromeLabs/third-party-capital/) under the hood to fetch the best loading recommendations.

Or something like that


# Usage

## Google Analytics

The `GoogleAnalytics` component loads a Google Analytics 4 tag into the page and off-loaded the execution to a web worker using Partytown. This includes the 83 kB gtag script that is required.

Choose a reason for hiding this comment

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

Suggested change
The `GoogleAnalytics` component loads a Google Analytics 4 tag into the page and off-loaded the execution to a web worker using Partytown. This includes the 83 kB gtag script that is required.
The `GoogleAnalytics` component loads a Google Analytics 4 tag into the page and off-loads the execution to a web worker using Partytown. This includes the 83 kB gtag script that is required.

Also I think we'll have to mention that nextScriptWorkers will have to be enabled for this to work (otherwise it'll fallback to loading after the page is interactive) and that it's not yet supported in the App directory: https://nextjs.org/docs/pages/building-your-application/optimizing/scripts#offloading-scripts-to-a-web-worker-experimental


```js
import { GoogleAnalytics } from '@next/third-parties'

export default function Page() {
return <GoogleAnalytics id="G-7GY583TNN9" />
}
```

## Youtube Embed

The `YoutubeEmbed` component is used to load and display a Youtube embed. This component loads approximately 224× faster by using [lite-youtube-embed](https://github.com/paulirish/lite-youtube-embed) under the hood.

Choose a reason for hiding this comment

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

Suggested change
The `YoutubeEmbed` component is used to load and display a Youtube embed. This component loads approximately 224× faster by using [lite-youtube-embed](https://github.com/paulirish/lite-youtube-embed) under the hood.
The `YoutubeEmbed` component is used to load and display a YouTube embed. This component loads approximately 224× faster by using [lite-youtube-embed](https://github.com/paulirish/lite-youtube-embed) under the hood.


```js
import { YoutubeEmbed } from '@next/third-parties'

export default function Page() {
return <YoutubeEmbed videoid="ogfYd705cRs" height={400} />
}
```

## Google Maps Embed

The `GoogleMapsEmbed` component can be used to lazy load a Google Maps Embed using the loading attribute.

Choose a reason for hiding this comment

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

Suggested change
The `GoogleMapsEmbed` component can be used to lazy load a Google Maps Embed using the loading attribute.
The `GoogleMapsEmbed` component can be used to include a [Google Maps Embed](https://developers.google.com/maps/documentation/embed/get-started) to your page. By default, it uses the `loading` attribute to lazy-load below the fold.


```js
import { GoogleMapsEmbed } from '@next/third-parties'

export default function Page() {
return (
<GoogleMapsEmbed
height={200}
width="100%"
mapMode="place"
parameters="q=Brooklyn+Bridge,New+York,NY"
/>
)
}
```

Check out the [demo](https://test-next-script-housseindjirdeh.vercel.app/)

Choose a reason for hiding this comment

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

Suggested change
Check out the [demo](https://test-next-script-housseindjirdeh.vercel.app/)
To get a better idea of how these components work, take a look at this [demo](https://test-next-script-housseindjirdeh.vercel.app/).

56 changes: 56 additions & 0 deletions packages/third-parties/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* This is an autogenerated file by update-third-parties.js
*/
import React from 'react'
import Script from 'next/script'

import Base from './base'

// Install a Google Analytics tag on your website
export function GoogleAnalytics(args: any) {
return (
<Base>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${args.id}`}
strategy="worker"
/>
<Script
id="google-analytics"
strategy="worker"
dangerouslySetInnerHTML={{
__html: `window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','${args.id}')`,
}}
/>
</Base>
)
}

// Embed a Google Maps embed on your webpage
export function GoogleMapsEmbed(args: any) {
return (
<Base
height={args.height || null}
width={args.width || null}
content={`<iframe loading="lazy" src="https://www.google.com/maps/embed/v1/${args.mapMode}?key=${args.apiKey}&${args.parameters}" width=${args.width} height=${args.height} style=${args.style} loading=${args.loading} allowfullscreen=${args.allowfullscreen} referrerpolicy="no-referrer-when-downgrade"></iframe>`}
></Base>
)
}

// Embed a YouTube embed on your webpage.
export function YoutubeEmbed(args: any) {
return (
<Base
height={args.height || null}
width={args.width || null}
content={`<lite-youtube videoid=${args.videoid} playlabel=${args.playlabel}></lite-youtube>`}
>
<Script
src={`https://cdn.jsdelivr.net/gh/paulirish/lite-youtube-embed@master/src/lite-yt-embed.js`}
strategy="lazyOnload"
stylesheets={[
'https://cdn.jsdelivr.net/gh/paulirish/lite-youtube-embed@master/src/lite-yt-embed.css',
]}
/>
</Base>
)
}