react ssr方法
react ssr 框架next.js開發個人網站心得
?舍我其誰2013666?關注
?0.1?2018.07.28 13:27*?字數 1231?閱讀 395評論 0喜歡 2
next-blog
項目介紹
利用react服務端框架next.js寫的博客,喜歡就給個Star支持一下。
https://github.com/Weibozzz/next-blog
線上地址:?http://www.liuweibo.cn
本項目使用next.js經驗分享:http://www.liuweibo.cn/p/206
軟件架構
軟件架構說明
react.js next.js antd mysql node koa2 fetch
網站使用技術
- 前端:React(16.x) Next.js antd-design fetch Less
- 后端:node框架koa和mysql (目前前后端分離,這里只負責寫接口,和平常的ajax獲取接口一樣,這里就不開放源碼了)
- 網站目的:業余學習,記錄技術文章,學以致用
- 網站功能
- markdown發布文章
- 修改文章(增刪改查)
- 用戶評論
- 上傳圖片到七牛云存儲
安裝教程
雖然是服務端渲染,但是也要調用接口,所以需要調用后端的接口
進入config文件夾下的env.js的isShow設置為true,這里只是調用了我自己線上的接口,當然你
只能看不能修改接口哦。如果為false則調不到接口,需要自己去寫接口。
使用說明
- 關于演示不能上傳圖片,不能發表文章或者修改屬于正常情況,因為只是為了展示。
- 關于路看不到發布文章路由和后臺管理也屬于正常情況,可以修改代碼展示路由效果。
網站截圖
詳情頁
?http://pd96wjt4m.bkt.clouddn.com/image/common/detail_1536836727000_459470_1536836749510.png
列表頁
?http://pd96wjt4m.bkt.clouddn.com/image/common/list_1536836639000_822188_1536836780676.png
編輯頁面和發布文章,上傳圖片到七牛云
?http://pd96wjt4m.bkt.clouddn.com/image/common/edit_1536836607000_802376_1536836825962.png
網站技術介紹
完全借助于?next.js?開發的個人網站,線上地址?http://www.liuweibo.cn?總結一下開發完成后的心得和使用體會。gtihub源碼https://github.com/Weibozzz/next-blog。喜歡就給個Star支持一下。
為什么使用服務器端渲染(SSR)?
- 網站是要推廣的,所以需要更好的 SEO,搜索引擎可以抓取完整頁面
- 訪問速度,更快的加載靜態頁面
網站使用技術
- 前端:React(16.x) Next.js antd-design fetch Less
- 后端:node框架koa和mysql (目前前后端分離,這里只負責寫接口,和平常的ajax獲取接口一樣,這里就不開放源碼了)
- 網站目的:業余學習,記錄技術文章,學以致用
- 網站功能
- 發布文章
- 修改文章(增刪改查)
- 用戶評論
源碼剖析
這里就只講重點了
入口文件server.js
這里用的官方提供的express,同時開啟gzip壓縮
const express = require('express') const next = require('next')const compression = require('compression') const dev = process.env.NODE_ENV !== 'production' const app = next({ dev }) const handle = app.getRequestHandler() let port= dev?4322:80app.prepare().then(() => {const server = express()if (!dev) {server.use(compression()) //gzip}//文章二級頁面server.get('/p/:id', (req, res) => {const actualPage = '/detail'const queryParams = { id: req.params.id }app.render(req, res, actualPage, queryParams)})server.get('*', (req, res) => {return handle(req, res)})server.listen(port, (err) => {if (err) throw errconsole.log('> Ready on http://localhost ' port)})}).catch((ex) => {process.exit(1)})page根組件_app.js
用于傳遞redux數據,store就和普通react用法一樣了,還有header和footer可以放在這里,同理還有_err.js用于處理404頁面
import App, {Container} from 'next/app' import React from 'react' import {withRouter} from 'next/router' // 接入next的router import withReduxStore from '../lib/with-redux-store' // 接入next的redux import {Provider} from 'react-redux'class MyApp extends App {render() {const {Component, pageProps, reduxStore, router: {pathname}} = this.props;return (<Container><Provider store={reduxStore}><Component {...myPageProps} /></Provider></Container>)} }export default withReduxStore(withRouter(MyApp))網站的服務端渲染頁面Blog頁面
- link用于跳轉頁面,利用as把原本的http://***.com?id=1變為漂亮的 /id/1
- head可以嵌套meta標簽進行seo
- 配置不需要seo的組件
靜態資源
根目錄創建static文件夾,這里是強制要求,否則加載不到靜態資源
配置antd和主題并且按需加載
主題配置
antd-custom.less
@primary-color: #722ED0;@layout-header-height: 40px; @border-radius-base: 0px;styles.less
@import "~antd/dist/antd.less"; @import "./antd-custom.less";最后統一配置在公共head
<Head><meta charSet="utf-8"/><meta httpEquiv="X-UA-Compatible" content="IE=edge, chrome=1"/><meta name="viewport"content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/><meta name="renderer" content="webkit"/><meta httpEquiv="description" content="劉偉波-天天向上"/><meta name="author" content="劉偉波,liuweibo"/><link rel='stylesheet' href='/_next/static/style.css'/><link rel='stylesheet' type='text/css' href='/static/nprogress.css' /><link rel='shortcut icon' type='image/x-icon' href='/static/favicon.ico' /></Head>按需加載配置
.babelrc文件
{"presets": ["next/babel"],"plugins": ["transform-decorators-legacy",["import",{"libraryName": "antd","style": "less"}]] }next.config.js文件配置
const withLess = require('@zeit/next-less')module.exports = withLess({lessLoaderOptions: {javascriptEnabled: true,cssModules: true,}} )頁面css
感覺和vue的scope一樣,style的jsx,加了global為全局,否則只在這里生效
render() {return (<Container><Provider store={reduxStore}><Component {...myPageProps} /></Provider><style jsx global>{`.fl{float: left; } .fr{float: right; }`}</style></Container>)頁面頂部加載進度條
import Router from 'next/router' import NProgress from 'nprogress'Router.onRouteChangeStart = (url) => {NProgress.start() } Router.onRouteChangeComplete = () => NProgress.done() Router.onRouteChangeError = () => NProgress.done()markdown發表文章和代碼高亮
使用只需要marked('放入markdown字符串');
import marked from 'marked' import hljs from 'highlight.js';hljs.configure({tabReplace: ' ',classPrefix: 'hljs-',languages: ['CSS', 'HTML, XML', 'JavaScript', 'PHP', 'Python', 'Stylus', 'TypeScript', 'Markdown'] }) marked.setOptions({highlight: (code) => hljs.highlightAuto(code).value,gfm: true,tables: true,breaks: false,pedantic: false,sanitize: true,smartLists: true,smartypants: false });學累了,來個圖放松下
[圖片上傳失敗...(image-b0b5dd-1536913267870)]
參與貢獻
遺留問題
總結
以上是生活随笔為你收集整理的react ssr方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++中对多态的理解
- 下一篇: 排序算法之希尔排序(C/C++)