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 GoogleMaps and Youtube embed components #52909

Merged
merged 70 commits into from Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
4beb690
Setting up third-parties package
janicklas-ralph Jun 12, 2023
4590570
Remove unwanted code
janicklas-ralph Jun 12, 2023
857a189
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 15, 2023
ddf4e41
Remove GA. Update import paths
janicklas-ralph Jun 16, 2023
20ba09d
Add comments
janicklas-ralph Jun 20, 2023
d4933f0
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 21, 2023
38a8e96
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 21, 2023
1af8446
Fix lint
janicklas-ralph Jun 22, 2023
16a7bac
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 22, 2023
8f51c4c
Fix lint
janicklas-ralph Jun 22, 2023
ebc5710
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 22, 2023
5881260
Fix review comments
janicklas-ralph Jun 26, 2023
83e6ff0
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 26, 2023
0fe1533
Move outdent and prettier into devDependencies since we use them only…
janicklas-ralph Jun 26, 2023
ae5ed8c
Update lock file
janicklas-ralph Jun 27, 2023
7e9e0d9
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 27, 2023
ae7f11a
Marking third-parties as private
janicklas-ralph Jun 29, 2023
5e2180b
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jun 29, 2023
f5a9b50
remove console.log
huozhi Jul 6, 2023
7f784cb
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 6, 2023
2d4cd2c
Merge branch 'tpc' of github.com:janicklas-ralph/next.js into tpc
janicklas-ralph Jul 6, 2023
f1066a1
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 11, 2023
5e5da7d
Modifying stylesheet loading. Loading stylesheets only for appDir
janicklas-ralph Jul 12, 2023
93ed720
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 12, 2023
ca57ab1
Remove unused test
janicklas-ralph Jul 12, 2023
d7f8c8e
Handle stylesheet for pages dir
janicklas-ralph Jul 12, 2023
07b5a4e
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 12, 2023
1cd8274
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 12, 2023
912b3ef
Update lock file
janicklas-ralph Jul 12, 2023
aabab89
fix error
janicklas-ralph Jul 12, 2023
ba5ea22
fix types
janicklas-ralph Jul 12, 2023
ba00c27
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 13, 2023
bb48cf1
refactor stylesheet loading with ReactDOM.preinit
janicklas-ralph Jul 14, 2023
de1a007
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 14, 2023
621f0a7
Trigger build
janicklas-ralph Jul 14, 2023
c22aa26
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 17, 2023
9bb65ba
Fix lint
janicklas-ralph Jul 17, 2023
4090fca
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 17, 2023
e84f165
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 18, 2023
5e1cf89
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 18, 2023
f4636fd
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 19, 2023
8673256
Adding GoogleMaps and Youtube embed components
janicklas-ralph Jul 19, 2023
4e4ce54
Update readme
janicklas-ralph Jul 19, 2023
783849b
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 19, 2023
d4f6b24
Fix review comments
janicklas-ralph Jul 19, 2023
ad7a98e
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 19, 2023
adbfbfc
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 20, 2023
556342d
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 25, 2023
08eb415
Update third-parties components and tests
janicklas-ralph Jul 25, 2023
2c470f7
Remove integration tests and move to e2e
janicklas-ralph Jul 25, 2023
b96a0f8
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 31, 2023
cf5f23e
Updating tests with createNextDescribe
janicklas-ralph Jul 31, 2023
4de98c4
Update tpc version
janicklas-ralph Jul 31, 2023
a30ed35
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 31, 2023
c97b2f8
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 31, 2023
0b2dd4f
Update package.json
janicklas-ralph Jul 31, 2023
43a0392
Update package.json
janicklas-ralph Jul 31, 2023
4ef700b
Update tsconfig compile options
janicklas-ralph Jul 31, 2023
a097f14
Fix test cases
janicklas-ralph Jul 31, 2023
8e691a9
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Jul 31, 2023
216878d
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Aug 1, 2023
19fa520
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Aug 1, 2023
79dd2b1
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Aug 1, 2023
5007ce0
Update readme
janicklas-ralph Aug 2, 2023
4f5c760
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Aug 2, 2023
3b23593
Remove extra loading prop
janicklas-ralph Aug 2, 2023
b05f5f1
Fix test
janicklas-ralph Aug 2, 2023
b673935
Merge branch 'canary' of github.com:vercel/next.js into tpc
janicklas-ralph Aug 7, 2023
3d1e694
Update tests
janicklas-ralph Aug 7, 2023
ac95f83
Merge branch 'canary' into tpc
kodiakhq[bot] Aug 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/third-parties/README.md
@@ -0,0 +1,43 @@
# Experimental `@next/third-parties`

`@next/third-parties` is a collection of components and utilities that can be used to efficiently load third-party libraries into your Next.js application.

> Note: `@next/third-parties` is still experimental and under active development.

# Usage

## Google Third-Parties

### YouTube Embed

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

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

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

Choose a reason for hiding this comment

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

Not super important, and not relevant for this PR, but @janicklas-ralph do you think it would be better to have this component named <YouTubeEmbed> instead? If so, I can update Third Party Capital's export and we can submit an additional PR that just runs the build script again

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you're using version 1.0.18 now, I think it's easier if we just update the export naming to YouTubeEmbed now to match Third Party Capital instead of pushing out a new PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

@janicklas-ralph Are we planning on changing this in this PR or a follow-up?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like we downgraded back to 1.0.17 in this PR. I think the plan is to do this in a follow-up PR once upgrading to the latest version.

}
```

### Google Maps Embed

The `GoogleMapsEmbed` component can be used to add 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/google'

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

To get a better idea of how these components work, take a look at this [demo](https://test-next-script-housseindjirdeh.vercel.app/). <!--- TODO: Replace with a better demo page -->
8 changes: 6 additions & 2 deletions packages/third-parties/package.json
Expand Up @@ -6,13 +6,17 @@
"url": "vercel/next.js",
"directory": "packages/third-parties"
},
"main": "dist/index.js",
"exports": {
"./google": "./dist/google/index.js"
},
"files": [
"dist"
],
"license": "MIT",
"scripts": {
"manual-build": "rm -rf dist && tsc -d -p tsconfig.json && node scripts/update-third-parties",
"build": "rm -rf dist && tsc -d -p tsconfig.json",
"prepublishOnly": "cd ../../ && turbo run build",
"updateThirdParties": "rm -rf src/**/index.tsx && node scripts/update-third-parties",
"dev": "tsc -d -w -p tsconfig.json",
"typescript": "tsec --noEmit -p tsconfig.json"
},
Expand Down
20 changes: 14 additions & 6 deletions packages/third-parties/scripts/update-third-parties.js
Expand Up @@ -23,7 +23,11 @@ function generateComponent(thirdParty) {
let props = ''

if (stylesheets?.length > 0) {
props += ` stylesheets={${JSON.stringify(stylesheets)}}`
// TODO: Remove ts-ignore after new <Script> component is published
// New <Script> accepts stylesheet as a param. Otherwise it throws error
props += `
// @ts-ignore
stylesheets={${JSON.stringify(stylesheets)}}`
}

return scripts
Expand Down Expand Up @@ -57,15 +61,18 @@ function generateComponent(thirdParty) {
AllThirdParties[thirdParty]

thirdPartyFunctions += outdent`

// ${description}
export function ${thirdParty}(args: any) {
export function ${thirdParty}(args: Types.${thirdParty}) {
return (
<Base
<ThirdPartyScriptEmbed
${content ? 'height={args.height || null}' : ''}
${content ? 'width={args.width || null}' : ''}
${content ? `content={\`${content}\`}` : ''}>
${content ? `content={\`${content}\`}` : ''}
dataNtpc="${thirdParty}"
>
${scripts?.length > 0 ? insertScripts(id, scripts, stylesheets) : ''}
</Base>
</ThirdPartyScriptEmbed>
)
}
`
Expand Down Expand Up @@ -97,7 +104,8 @@ function generateComponent(thirdParty) {
import React from 'react'
import Script from 'next/script'

import Base from './base'
import ThirdPartyScriptEmbed from '../ThirdPartyScriptEmbed'
import * as Types from '../types/${dir}'
`
for (const thirdParty of Object.values(config)) {
thirdPartyFunctions += generateComponent(thirdParty)
Expand Down
@@ -1,16 +1,20 @@
import React from 'react'

export default function Base({
content,
height = null,
width = null,
children,
}: {
export type ScriptEmbed = {
content?: string
height?: number | null
width?: number | null
children?: React.ReactElement | React.ReactElement[]
}) {
dataNtpc?: string
}

export default function ThirdPartyScriptEmbed({
content,
height = null,
width = null,
children,
dataNtpc = '',
}: ScriptEmbed) {
return (
<>
{/* insert script children */}
Expand All @@ -22,6 +26,7 @@ export default function Base({
height: height != null ? `${height}px` : 'auto',
width: width != null ? `${width}px` : 'auto',
}}
data-ntpc={dataNtpc}
dangerouslySetInnerHTML={{ __html: content }}
/>
)}
Expand Down
40 changes: 40 additions & 0 deletions packages/third-parties/src/google/index.tsx
@@ -0,0 +1,40 @@
/**
* This is an autogenerated file by update-third-parties.js
*/
import React from 'react'
import Script from 'next/script'

import ThirdPartyScriptEmbed from '../ThirdPartyScriptEmbed'
import * as Types from '../types/google'

// Embed a Google Maps embed on your webpage
export function GoogleMapsEmbed(args: Types.GoogleMapsEmbed) {
return (
<ThirdPartyScriptEmbed
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} allowfullscreen=${args.allowfullscreen} referrerpolicy="no-referrer-when-downgrade"></iframe>`}
dataNtpc="GoogleMapsEmbed"
></ThirdPartyScriptEmbed>
)
}
// Embed a YouTube embed on your webpage.
export function YoutubeEmbed(args: Types.YoutubeEmbed) {
return (
<ThirdPartyScriptEmbed
height={args.height || null}
width={args.width || null}
content={`<lite-youtube videoid=${args.videoid} playlabel=${args.playlabel}></lite-youtube>`}
dataNtpc="YoutubeEmbed"
>
<Script
src={`https://cdn.jsdelivr.net/gh/paulirish/lite-youtube-embed@master/src/lite-yt-embed.js`}
strategy="lazyOnload"
// @ts-ignore
stylesheets={[
'https://cdn.jsdelivr.net/gh/paulirish/lite-youtube-embed@master/src/lite-yt-embed.css',
]}
/>
</ThirdPartyScriptEmbed>
)
}
17 changes: 17 additions & 0 deletions packages/third-parties/src/types/google.ts
@@ -0,0 +1,17 @@
export type GoogleMapsEmbed = {
height?: number
width?: number
mapMode: 'place' | 'view' | 'directions' | 'streetview' | 'search'
apiKey: string
parameters: string
style: string
allowfullscreen: boolean
loading: 'eager' | 'lazy'
}

export type YoutubeEmbed = {
height?: number
width?: number
videoid: string
playlabel?: string
}
11 changes: 4 additions & 7 deletions packages/third-parties/tsconfig.json
Expand Up @@ -4,15 +4,12 @@
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"target": "es2020",
"lib": ["dom"],
"downlevelIteration": true,
"preserveWatchOutput": true,
"target": "es2019",
"outDir": "dist",
"jsx": "react",
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"moduleResolution": "Node16"
"resolveJsonModule": true,
"module": "commonjs",
"rootDir": "src"
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/third-parties/app/google-maps-embed/page.js
@@ -0,0 +1,18 @@
import { GoogleMapsEmbed } from '@next/third-parties/google'

const Page = () => {
return (
<div class="container">
<h1>Google Maps Embed</h1>
<GoogleMapsEmbed
apiKey="XYZ"
height={200}
width="100%"
mapMode="place"
parameters="q=Brooklyn+Bridge,New+York,NY"
/>
</div>
)
}

export default Page
7 changes: 7 additions & 0 deletions test/e2e/app-dir/third-parties/app/layout.tsx
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/third-parties/app/youtube-embed/page.js
@@ -0,0 +1,12 @@
import { YoutubeEmbed } from '@next/third-parties/google'

const Page = () => {
return (
<div class="container">
<h1>Youtube Embed</h1>
<YoutubeEmbed videoid="ogfYd705cRs" height={400} />
</div>
)
}

export default Page
32 changes: 32 additions & 0 deletions test/e2e/app-dir/third-parties/basic.test.ts
@@ -0,0 +1,32 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'@next/third-parties basic usage',
{
files: __dirname,
dependencies: {
'@next/third-parties': 'canary',
},
},
({ next }) => {
it('renders YoutubeEmbed', async () => {
const $ = await next.render$('/youtube-embed')

const baseContainer = $('[data-ntpc="YoutubeEmbed"]')
const youtubeContainer = $('lite-youtube')
expect(baseContainer.length).toBe(1)
expect(youtubeContainer.length).toBe(1)
})

it('renders GoogleMapsEmbed', async () => {
const $ = await next.render$('/google-maps-embed')

const baseContainer = $('[data-ntpc="GoogleMapsEmbed"]')
const mapContainer = $(
'[src="https://www.google.com/maps/embed/v1/place?key=XYZ&q=Brooklyn+Bridge,New+York,NY"]'
)
expect(baseContainer.length).toBe(1)
expect(mapContainer.length).toBe(1)
})
}
)
32 changes: 32 additions & 0 deletions test/e2e/third-parties/index.test.ts
@@ -0,0 +1,32 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'@next/third-parties basic usage',
{
files: __dirname,
dependencies: {
'@next/third-parties': 'canary',
},
},
({ next }) => {
it('renders YoutubeEmbed', async () => {
const $ = await next.render$('/youtube-embed')

const baseContainer = $('[data-ntpc="YoutubeEmbed"]')
const youtubeContainer = $('lite-youtube')
expect(baseContainer.length).toBe(1)
expect(youtubeContainer.length).toBe(1)
})

it('renders GoogleMapsEmbed', async () => {
const $ = await next.render$('/google-maps-embed')

const baseContainer = $('[data-ntpc="GoogleMapsEmbed"]')
const mapContainer = $(
'[src="https://www.google.com/maps/embed/v1/place?key=XYZ&q=Brooklyn+Bridge,New+York,NY"]'
)
expect(baseContainer.length).toBe(1)
expect(mapContainer.length).toBe(1)
})
}
)
18 changes: 18 additions & 0 deletions test/e2e/third-parties/pages/google-maps-embed.js
@@ -0,0 +1,18 @@
import { GoogleMapsEmbed } from '@next/third-parties/google'

const Page = () => {
return (
<div class="container">
<h1>Google Maps Embed</h1>
<GoogleMapsEmbed
apiKey="XYZ"
height={200}
width="100%"
mapMode="place"
parameters="q=Brooklyn+Bridge,New+York,NY"
/>
</div>
)
}

export default Page
12 changes: 12 additions & 0 deletions test/e2e/third-parties/pages/youtube-embed.js
@@ -0,0 +1,12 @@
import { YoutubeEmbed } from '@next/third-parties/google'

const Page = () => {
return (
<div class="container">
<h1>Youtube Embed</h1>
<YoutubeEmbed videoid="ogfYd705cRs" height={400} />
</div>
)
}

export default Page