TwicPics / components

A Web component library that brings the power of TwicPics to your favorite web framework.
MIT License
53 stars 2 forks source link

twicpics component blank in production nextjs app #96

Closed Dakuan closed 4 months ago

Dakuan commented 4 months ago

v: 0.28.0

everything fine in development:

image

but broken in production:

image https://photos.dombarker.co.uk/posts/sgN9ll71ju/shoveler

strange 404 that might be related (url looks a bit like the end of a twicpics url)

image

have tried:

was working just fine yesterday 😂

Dakuan commented 4 months ago

ignore me, i changed a env var name and forgot. friday 😂

mbgspcii commented 4 months ago

Hi @Dakuan

Yes... It's definitely not normal.

Can you tell me which version of the components you used when it worked?

mbgspcii commented 4 months ago

Ah, well, i prefer that (😉).

No problem.

But I can see that the undefined (null should be ?) case is not well managed.

We'll add a more explicit message to handle that case.

Thanks for opening the issue and have a nice day.

Happy coding.

Dakuan commented 4 months ago

thanks, yes i think if the domain is missing / invalid it should error hard, like when you miss an API key in an SDK or some such

mbgspcii commented 4 months ago

I see your image in production.

2 comments:

Dakuan commented 4 months ago

would love to have used TwicImage but I need to maintain the original aspect ratio and didnt see an easy way to do that - ditto for a masonry layout for the lists pages, photographers dont like having their compositions messed with!

mbgspcii commented 4 months ago

I see. Something to test if you want: On server side call this route: https://fortherecord.twic.pics/production/users/5a5ec10e-4cba-44e6-af46-52d39e75705c/2pi3cid0-DSC01526-ARW_DxO_DeepPRIMEXD.jpg?twic=v1/inspect

Inspect will return a json with intrinsic sizes of your asset.

The you can use TwicImg giving to ratio prop the value : 3728/2485 (in that particular case) Of course you'll have to make an additional request but i think it is worth a try.

Dakuan commented 4 months ago

thats super useful, my other option was do some lambda thing on image upload and store the aspect myself! thanks very much!

Dakuan commented 4 months ago

that works brilliantly, i make that api call in a pre-insert model hook et voila:

image

mbgspcii commented 4 months ago

Wow super nice. Thanks for the feedback.

Please, could you share with us the url of your site when it will be in production?

Well done in any case.

Be seeing you.

Dakuan commented 4 months ago

we are all in production now :)

Here is what I did...

create a utility to get the ratio:

import { toFullTwicPicsUrl } from "./toFullTwicPicsUrl";

export const fetchRatioFromTwicPics = async (storageUrl: string) => {
    const twicUrl = `${toFullTwicPicsUrl(storageUrl)}?twic=v1/inspect`;

    const res = await fetch(twicUrl);
    const {
        input: { width, height },
    } = await res.json();

    return `${width}/${height}`;
};

add a ratio col to my images table, and populate for existing images:

import { fetchRatioFromTwicPics } from "@/lib/services/twicpics";
import type { Knex } from "knex";

const tableName = "images";

export async function up(knex: Knex): Promise<void> {
  await knex.schema.alterTable(tableName, (table) => {
    table.string("ratio");
  });

  const images = await knex
    .table(tableName)
    .select<{ url: string; id: string }[]>("url", "id");

  for (let i = 0; i < images.length; i++) {
    const { id, url } = images[i];

    const ratio = await fetchRatioFromTwicPics(url);

    await knex.table(tableName).update({ ratio }).where({ id });
  }
}

export async function down(knex: Knex): Promise<void> {
  await knex.schema.alterTable(tableName, (table) => {
    table.dropColumn("ratio");
  });
}

Get ratio from twicpics when saving my image model for new images:

import { QueryContext } from "objection";
import { RawExif } from "./RawExif";
import { ForTheRecordModel } from "./model";
import { fetchRatioFromTwicPics } from "../services/twicpics";

export class Image extends ForTheRecordModel {
  static tableName = "images";
  id!: string;
  url!: string;
  userId!: string;
  rawExif!: RawExif;
  ratio!: string;

  async $beforeInsert(_queryContext: QueryContext): Promise<any> {
    this.ratio = await fetchRatioFromTwicPics(this.url);
  }
}

use the ratio col in the front end:

  <Masonry className="flex" columnClassName="pl-2 md:pl-4" breakpointCols={breaks}>
    {posts.map((p) => {
      return (
        <Link
          className="pb-2 md:pb-4 block"
          key={p.id}
          href={toPostsUrl(p.nanoId, p.species.commonName)}
        >
          <TwicImg
            src={toTwicPicsPath(p.image.url)}
            ratio={p.image.ratio}
          />
        </Link>
      );
    })}
    <div ref={ref} />
  </Masonry>

used for masony grid: https://photos.dombarker.co.uk/ (you can see the old grid too for comparison: https://photos.dombarker.co.uk/?grid=grid) for where images should not be cropped: https://photos.dombarker.co.uk/posts/5xhWSalsPh/goshawk

things that would be really helpful in that inspect call:

other feedback:

mbgspcii commented 4 months ago

Thank you for your comments. They are very valuable and well noted.

Dakuan commented 4 months ago

not at all, love the product! the inspect call is especially exciting as easy access to image metadata adds all kinds of creative possiblities!