SSR embeds

main
Mike Fix 5 years ago
parent 37c546437b
commit f3e912d999

@ -18,7 +18,6 @@
"Location": "https://www.notion.so/TERMS-OF-USE-ff2ce22a7e9848c89c6be46b44297583" "Location": "https://www.notion.so/TERMS-OF-USE-ff2ce22a7e9848c89c6be46b44297583"
} }
}, },
{ "src": "^/embed(.*)", "dest": "/embed.html" },
{ {
"src": "^/service-worker.js$", "src": "^/service-worker.js$",
"dest": "/_next/static/service-worker.js", "dest": "/_next/static/service-worker.js",

@ -0,0 +1,42 @@
import React from 'react'
import Router from 'next/router'
import EmbedPage from './index'
import ApiContext from '../../components/ApiContext'
import api from '../../lib/api'
class EmbedIdPage extends React.PureComponent {
static contextType = ApiContext
static async getInitialProps({ req, res, query }) {
const path = query.id
const parameter = path && path.length >= 19 && path.indexOf('.') < 0 && path
let snippet
if (parameter) {
const host = req ? req.headers.host : undefined
snippet = await api.snippet.get(parameter, { host })
if (snippet && snippet.gist_id) {
return { snippet }
}
// 404 Not found
if (res) {
res.writeHead(302, {
Location: '/embed'
})
res.end()
} else {
Router.push('/embed')
}
}
return {}
}
render() {
return <EmbedPage {...this.props} />
}
}
export default EmbedIdPage

@ -4,11 +4,11 @@ import Head from 'next/head'
import { withRouter } from 'next/router' import { withRouter } from 'next/router'
// Ours // Ours
import ApiContext from '../components/ApiContext' import ApiContext from '../../components/ApiContext'
import { StylesheetLink, CodeMirrorLink, MetaTags } from '../components/Meta' import { StylesheetLink, CodeMirrorLink, MetaTags } from '../../components/Meta'
import Carbon from '../components/Carbon' import Carbon from '../../components/Carbon'
import { DEFAULT_CODE, DEFAULT_SETTINGS } from '../lib/constants' import { DEFAULT_CODE, DEFAULT_SETTINGS } from '../../lib/constants'
import { getRouteState } from '../lib/routing' import { getRouteState } from '../../lib/routing'
const Page = props => ( const Page = props => (
<React.Fragment> <React.Fragment>
@ -46,23 +46,11 @@ class Embed extends React.Component {
snippet = {} snippet = {}
async componentDidMount() { async componentDidMount() {
const { queryState, parameter } = getRouteState(this.props.router) const { queryState } = getRouteState(this.props.router)
if (parameter) {
const snippet = await this.context.snippet.get(parameter)
if (snippet) {
this.snippet = snippet
}
}
// TODO fix state!
this.i = setTimeout(() => {
this.setState(s => ({ key: s.key + 1 }))
}, 10)
this.setState( this.setState(
{ {
...this.snippet, ...this.props.snippet,
...queryState, ...queryState,
copyable: queryState.copy !== false, copyable: queryState.copy !== false,
readOnly: queryState.readonly !== false, readOnly: queryState.readonly !== false,
@ -72,10 +60,6 @@ class Embed extends React.Component {
) )
} }
componentWillUnmount() {
clearInterval(this.i)
}
ref = React.createRef() ref = React.createRef()
postMessage = () => { postMessage = () => {
@ -109,9 +93,9 @@ class Embed extends React.Component {
render() { render() {
return ( return (
<Page theme={this.state.theme}> <Page theme={this.state.theme}>
{this.state.mounted && ( <div hidden={!this.state.mounted}>
<Carbon <Carbon
key={this.state.key} key={this.state.mounted}
ref={this.ref} ref={this.ref}
config={this.state} config={this.state}
readOnly={this.state.readOnly} readOnly={this.state.readOnly}
@ -120,7 +104,7 @@ class Embed extends React.Component {
> >
{this.state.code} {this.state.code}
</Carbon> </Carbon>
)} </div>
<style jsx global> <style jsx global>
{` {`
.eliminateOnRender, .eliminateOnRender,
Loading…
Cancel
Save