This is a starter template for Learn Next.js.
- 约定式路由
- Link 标签会进行 prefetch
- css in link should add a tag
a
-
public 目录下添加 images 可以添加静态资源(图片)
use image component from
/next/image
, Images are lazy loaded by default.Resizing & optimizing images
1.也就是说默认开启优化 2.不论静态资源有多少图片都不会影响你的编译速度 3.只有出现在可视化窗口中时,才会加载(滚动加载优化)
-
Metadata
<title>
is part of the<head>
- Third-Party JavaScript 第三方插件
strategy
属性设为lazyOnload
可以懒加载onLoad
加载完后执行的回调函数
import Script from 'next/script' <Head> <title>First Post</title> </Head> <Script src="https://connect.facebook.net/en_US/sdk.js" strategy="lazyOnload" onLoad={() => console.log(`script loaded correctly, window.FB has been populated`) } />
-
css (css in js) Next.js built-in support
- styled-jsx
- CSS Modules
- Sass npm install sass
use css modules.module.scss
or.module.sass
-
global.css only inside pages/_app.js
-
postcss.config.js or tailwind.config.js
- disable JavaScript on chrome command + shift + p => javascript => disable javascript
tell me what do you see
- if disable Javascript on create-react-app ,you will see
You need to enable JavaScript to run this app.
Two forms of pre-rendering
-
Server-side rendering generation HTML on each request
-
Static Generation rendering generation HTML on build
Static Generation with Data you should use
getStaticProps
on the page, for example:export default function Home(props) { ... } export async function getStaticProps() { // Get external data from the file system, API, DB, etc. const data = ... // The value of the `props` key will be // passed to the `Home` component return { props: ... } }
getStaticPaths
get route pathgetStaticProps
get content by idStatically Generates /posts/a/b/c
//getStaticPaths ... return [ { params: { // Statically Generates /posts/a/b/c id: ['a', 'b', 'c'] } } //... ]
pages/api/hello.js
export default function handler(req, res) { res.status(200).json({ text: "Hello" }); }
http://localhost:3001/api/hello you will get
{text:'Hello'}
-
Preview Mode
CMS
???
-