I can successfully retrieve the meta data from a url using the below code and Metadata-scaper when it is hard coded. But how would I go about allowing the user to input the link using the text input and fetching the meta data on form submit?
I'm getting confused with passing data to the getStaticProps. Thank you any help be hugely appreciated.
import Head from 'next/head'
import { useState } from 'react';
import styles from '../styles/Home.module.css'
import getMetaData from 'metadata-scraper'
export async function getStaticProps(name) {
const url = "http://www.bbc.com"
const data = await getMetaData(url)
var urlData = JSON.stringify(data);
if (!urlData) {
return {
notFound: true,
}
}
return {
props: { urlData }, // will be passed to the page component as props
}
}
export default function Home(urlData) {
const [name, setName] = useState('');
const handleSubmit = e => {
e.preventDefault();
const data = {
name,
};
getStaticProps(name)
console.log(data);
};
const myData = JSON.parse(urlData.urlData)
return (
<div>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<form onSubmit={handleSubmit} className={styles.form}>
<label htmlFor="name">Name:</label>
<input
id="name"
type="text"
onChange={e => setName(e.target.value)}
/>
<button type="submit">Send</button>
</form>
</main>
{console.log(myData.title)}
<table>
<tr>
<th>title</th>
<th>Provider</th>
<th>Author</th>
<th>url</th>
<th>Language</th>
<th>Published</th>
</tr>
<tr>
<td>{myData.title}</td>
<td>{myData.provider}</td>
<td>{myData.author}</td>
<td>{myData.url}</td>
<td>{myData.language}</td>
<td>{myData.published}</td>
</tr>
</table>
</div>
);
}
Update 3/8/2021 - 15:35 GMT
I've separated out the API Routes to api/meta.js like so but how do I pass the link from the form there?
import getMetaData from 'metadata-scraper'
export default async function handler(req, res) {
try {
const url = 'www.bbc.com'
res.statusCode = 200;
const data = await getMetaData(url)
res.end(JSON.stringify(data));
console.log(data)
}
catch (error) {
res.json(error);
res.status(405).end();
}
}
Updated index.js
import Head from 'next/head'
import { useState } from 'react';
import styles from '../styles/Home.module.css'
import useSWR from 'swr'
export default function Home() {
const { data } = useSWR('/api/meta', fetch)
const [name, setName] = useState('');
const handleSubmit = e => {
e.preventDefault();
fetch('/api/meta', {
method: 'post',
body: JSON.stringify({
name: name,
})
})
};
return (
<div>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<form onSubmit={handleSubmit} className={styles.form}>
<label htmlFor="name">Name:</label>
<input
id="name"
type="text"
onChange={e => setName(e.target.value)}
/>
<button type="submit">Send</button>
</form>
</main>
</div>
);
}