Astro Support · Issue #306 · github/relative-time-element · GitHub
Skip to content

Astro Support #306

Open
Open

Description

@jdgaravito

I'm planning to use this on my astro project but the suggested implementation doesnt work, astro wont render any formated date at all.

Still getting the default April 1.

I dont see any way to import it into my server block or client. the tags relative-time are not doing anything.

Can you make it compatible so i can import it into my project like all the npm modules?
something like:

import { relative-time } from Relative-Time-Elements

Activity

  1. jdgaravito commented on Jan 24, 2025

    @jdgaravito
    Author

    this is the only way I made it work, but is because is making a call to github to download it wich is not performance friendly,

    ---
    
    const { formattedDate } = Astro.props;
    
    interface Props {
        formattedDate: string;
    }
    ---
    
    <relative-time datetime={formattedDate}>
        {formattedDate}
    </relative-time>
    
    <script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script>
    

    tried something like

    ---
    import '@github/relative-time-element';
    ---
    

    or using as a module

    <script type="module">
        import '@github/relative-time-element';
    </script>
    

    and i had no luck.

  2. Heyner128 commented on Feb 12, 2025

    @Heyner128

    I had the same problem with Angular, the problem is that the bundlers of the Frameworks remove the unused imports, and the component depends on executing the define static method of the component.

    The solution is to use the default export somewhere to force the bundler to include the component code.

    Example in Angular syntax:

    import RelativeTimeElement from '@github/relative-time-element'
    
    @Component({
      selector: 'my-component',
      imports: [],
      template: '<relative-time [attr.datetime]="myDate">April 1, 2024</relative-time>',
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    })
    export class MyComponent {
      myDate = new Date()
    
      constructor() {
        // Logs the default export to include the code...
        console.log(RelativeTimeElement)
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions