Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c40f184a
Commit
c40f184a
authored
Jan 26, 2018
by
Shaleen Jain
Committed by
Tim Neutkens
Jan 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extend with-data-prefetch to handle advanced use cases (#3525)
parent
8dd8e200
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
27 deletions
+39
-27
link.js
examples/with-data-prefetch/components/link.js
+36
-24
index.js
examples/with-data-prefetch/pages/index.js
+3
-3
No files found.
examples/with-data-prefetch/components/link.js
View file @
c40f184a
...
@@ -5,6 +5,34 @@ import { execOnce, warn } from 'next/dist/lib/utils'
...
@@ -5,6 +5,34 @@ import { execOnce, warn } from 'next/dist/lib/utils'
import
exact
from
'prop-types-exact'
import
exact
from
'prop-types-exact'
import
{
format
,
resolve
,
parse
}
from
'url'
import
{
format
,
resolve
,
parse
}
from
'url'
export
const
prefetch
=
async
(
href
)
=>
{
// if we're running server side do nothing
if
(
typeof
window
===
'undefined'
)
return
const
url
=
typeof
href
!==
'string'
?
format
(
href
)
:
href
const
{
pathname
}
=
window
.
location
const
parsedHref
=
resolve
(
pathname
,
url
)
const
{
query
}
=
typeof
href
!==
'string'
?
href
:
parse
(
url
,
true
)
const
Component
=
await
Router
.
prefetch
(
parsedHref
)
// if Component exists and has getInitialProps
// fetch the component props (the component should save it in cache)
if
(
Component
&&
Component
.
getInitialProps
)
{
const
ctx
=
{
pathname
:
href
,
query
,
isVirtualCall
:
true
}
await
Component
.
getInitialProps
(
ctx
)
}
}
// extend default next/link to customize the prefetch behaviour
// extend default next/link to customize the prefetch behaviour
export
default
class
LinkWithData
extends
Link
{
export
default
class
LinkWithData
extends
Link
{
// re defined Link propTypes to add `withData`
// re defined Link propTypes to add `withData`
...
@@ -33,32 +61,16 @@ export default class LinkWithData extends Link {
...
@@ -33,32 +61,16 @@ export default class LinkWithData extends Link {
// our custom prefetch method
// our custom prefetch method
async
prefetch
()
{
async
prefetch
()
{
// if the prefetch prop is not defined or
// if the prefetch prop is not defined do nothing
// we're running server side do nothing
if
(
!
this
.
props
.
prefetch
)
return
if
(
!
this
.
props
.
prefetch
)
return
if
(
typeof
window
===
'undefined'
)
return
const
url
=
typeof
this
.
props
.
href
!==
'string'
?
format
(
this
.
props
.
href
)
:
this
.
props
.
href
const
{
pathname
}
=
window
.
location
const
href
=
resolve
(
pathname
,
url
)
const
{
query
}
=
typeof
this
.
props
.
href
!==
'string'
?
this
.
props
.
href
:
parse
(
url
,
true
)
const
Component
=
await
Router
.
prefetch
(
href
)
// if withData prop is defined, Component exists and has getInitialProps
// if withData prop is defined
// fetch the component props (the component should save it in cache)
// prefetch with data
if
(
this
.
props
.
withData
&&
Component
&&
Component
.
getInitialProps
)
{
// otherwise just prefetch the page
const
ctx
=
{
pathname
:
href
,
query
,
isVirtualCall
:
true
}
if
(
this
.
props
.
withData
)
{
await
Component
.
getInitialProps
(
ctx
)
prefetch
(
this
.
props
.
href
)
}
else
{
super
.
prefetch
()
}
}
}
}
}
}
examples/with-data-prefetch/pages/index.js
View file @
c40f184a
import
Link
from
'../components/link'
import
Link
,
{
prefetch
}
from
'../components/link'
// we just render a list of 3 articles having 2 with prefetched data
// we just render a list of 3 articles having 2 with prefetched data
export
default
()
=>
(
export
default
()
=>
(
...
@@ -16,8 +16,8 @@ export default () => (
...
@@ -16,8 +16,8 @@ export default () => (
<
/Link
>
<
/Link
>
<
/li
>
<
/li
>
<
li
>
<
li
>
<
Link
href
=
'/article?id=3'
prefetch
withData
>
<
Link
href
=
'/article?id=3'
>
<
a
>
Article
3
<
/a
>
<
a
onMouseOver
=
{
e
=>
prefetch
(
'/article?id=3'
)}
>
Article
3
<
/a
>
<
/Link
>
<
/Link
>
<
/li
>
<
/li
>
<
/ul
>
<
/ul
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment